简体   繁体   English

在if语句中调用函数?

[英]Calling a function in an if statement?

I'm trying to write an if statement where if the user enters "yes" a game runs but when I cannot figure out how to do this, I can't find it online. 我正在尝试编写一条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? ")

This is because you have named both your variable for your input "game" and your function call "game". 这是因为您已为输入“游戏”和函数调用“游戏”都命名了变量。 rename one or the other and your code should work as intended. 重命名一个或另一个,您的代码应按预期工作。

If you are using Python2.*, You should use raw_input instead of input . 如果使用的是Python2。*,则应使用raw_input而不是input

And no matter what version of Python you are using, you should not use the same name for both the function and your variable. 而且,无论您使用的是哪个版本的Python,都不应对函数和变量使用相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM