[英]Calling a function in an if statement?
我正在尝试编写一条if语句,如果用户输入“是”,则将运行一个游戏,但是当我无法确定如何执行此操作时,将无法在线找到它。
userName = input("Hello, my name is Logan. What is yours? ")
userFeel = input("Hello " + userName + ", how are you? ")
if userFeel == "good":
print ("That's good to hear")
elif userFeel == "bad":
print ("Well I hope I can help with that")
q1 = input("Do you want to play a game? ")
if q1 == "yes":
print ("Alright, lets begin")
import random
print ("This is a guessing game")
randomNumber = random.randint(1, 100)
found = False
yes = "yes"
while not found:
userGuess = input('Your Guess: ') ; userGuess = int(userGuess)
if userGuess == randomNumber:
print ("You got it!")
found = True
elif userGuess>randomNumber:
print ("Guess Lower")
else:
print ("Guess Higher")
elif game == "no":
print ("No? Okay")
q2 = input("What do you want to do next? ")
这是因为您已为输入“游戏”和函数调用“游戏”都命名了变量。 重命名一个或另一个,您的代码应按预期工作。
如果使用的是Python2。*,则应使用raw_input
而不是input
。
而且,无论您使用的是哪个版本的Python,都不应对函数和变量使用相同的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.