簡體   English   中英

在if語句中調用函數?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM