我试图将变量设置为用户输入的字符串输入。之前我做过类似的类似操作,通过将变量设置为用户输入的整数输入并尝试复制它并将其从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 =设置为用户的字符串输入
答案 0 :(得分: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()