Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将变量设置为字符串输入python 3.5?_Python_String_Variables - Fatal编程技术网

如何将变量设置为字符串输入python 3.5?

如何将变量设置为字符串输入python 3.5?,python,string,variables,Python,String,Variables,我试图将一个变量设置为用户输入的字符串输入。我以前也做过类似的事情,将一个变量设置为用户输入的整数输入,并尝试复制它,然后将其从int()更改为str(),但没有成功。到目前为止,我所掌握的情况如下: import time def main(): print(". . .") time.sleep(1) playerMenu() Result(playerChoice) return def play(): playerChoice = st

我试图将一个变量设置为用户输入的字符串输入。我以前也做过类似的事情,将一个变量设置为用户输入的整数输入,并尝试复制它,然后将其从int()更改为str(),但没有成功。到目前为止,我所掌握的情况如下:

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=设置为用户字符串输入的任何值

您的函数返回值(好),但您没有对它们做任何操作(坏)。应将值存储在变量中,并将其传递给需要使用这些值的人:

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()

当您有
def Result():
时,如何调用
Result(playerChoice)
?您的代码是否编译,我在执行时看到许多错误?您是否知道函数中定义的变量是该函数的局部变量?此外,在代码中,您从未设置
playerChoice
(甚至在本地也没有设置,因为
play()
从未被任何人调用)。