2017-10-10 97 views
0

我想寫一個if語句,如果用戶輸入「是」遊戲運行,但當我無法弄清楚如何做到這一點,我無法在網上找到它。在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? ") 
+1

什麼不起作用? –

+1

您正在定義函數'game',但隨後將'game'重新分配給用戶輸入 – Wondercricket

+1

修復縮進,在Python中正確使用縮進至關重要。 – Barmar

回答

4

這是因爲你已經爲你的輸入遊戲和你的函數調用遊戲命名了兩個變量。重命名一個或另一個,你的代碼應該按預期工作。

2

如果您使用的是Python2。*,則應該使用raw_input而不是input

無論您使用的是什麼版本的Python,都不應該爲函數和變量使用相同的名稱。