在if语句中调用函数?

时间:2017-10-10 19:12:43

标签: python 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? ")

2 个答案:

答案 0 :(得分:4)

这是因为你已经为你的输入“游戏”和你的函数调用“游戏”命名了你的变量。重命名一个或另一个,您的代码应按预期工作。

答案 1 :(得分:2)

如果您使用的是Python2。*,则应使用raw_input代替input

无论您使用的是什么版本的Python,都不应对函数和变量使用相同的名称。