2016-04-28 10 views
-1

ユーザーが入力する文字列入力に変数を設定しようとしています。私は以前同様に、ユーザが入力した整数入力に変数を設定し、それをコピーしようとしましたが、int()からstr()に変更するだけで動作しませんでした。これまで私がこれまで持っていたことは次のとおりです。変数を文字列入力のpython 3.5に設定する方法は?

import time 

def main(): 
    print(". . .") 
    time.sleep(1) 
    playerMenu() 
    Result(playerChoice) 
    return 

def play(): 
    playerChoice = str(playerMenu()) 
    return playerChoice 


def playerMenu(): 
    print("So what will it be...") 
    meuuSelect = str("Red or Blue?") 
    return menuSelect 


def Result(): 
    if playerChoice == Red: 
     print("You Fascist pig >:c") 
    elif playerChoice == Blue: 
     print("QUICK, BEFORE YOU PASS OUT, WHAT DOES IT TASTE LIKE?!?") 
     return 

main() 

実行すると、playerChoiceが定義されていないことがわかります。私はplayerChoice =をユーザの文字列入力が何であったのか明確に設定して以来、私にこれを伝えている理由を理解できません。

+0

'def Result():'があるときに、どのように 'result(playerChoice)を呼び出すことができますか? –

+0

あなたのコードはコンパイルされますが、私はそうしている間に多くのエラーが発生します – piyushj

+0

関数内で定義された変数は、その関数にとってローカルなのですか?また、あなたのコードでは 'playerChoice'を決して決して決してしません(' play() 'は決して誰にも呼ばれないので)。 –

答えて

1

あなたの関数は値を返しますが、何もしません。値を変数に格納して、それを操作する必要のある人に渡す必要があります。

def main(): 
    print(". . .") 
    time.sleep(1) 
    choice = playerMenu() 
    Result(choice) 
    # no need for "return" at the end of a function if you don't return anything 

def playerMenu(): 
    print("So what will it be...") 
    menuSelect = input("Red or Blue?") # input() gets user input 
    return menuSelect 

def Result(choice): 
    if choice == "Red":     # Need to compare to a string 
     print("You Fascist pig >:c") 
    elif choice == "Blue": 
     print("QUICK, BEFORE YOU PASS OUT, WHAT DOES IT TASTE LIKE?!?") 

main() 
関連する問題