2016-04-28 101 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()