2016-04-28 49 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

你怎麼能叫'結果(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()