简体   繁体   中英

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.

   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 .

And no matter what version of Python you are using, you should not use the same name for both the function and your variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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