我有以Python編寫的代碼作爲一個項目。我已將計算機科學作爲GCSE。我對它很陌生,所以不知道如何解決這個錯誤。當我輸入我的第一個猜測,例如'5'時,它會說太高。然後一旦遊戲完成,它會說答案是7.這是一個我不斷嘗試解決但無法完成的錯誤。這裏是我用過的代碼:猜測遊戲錯誤?
import random
numberofGuesses = 0
print ("I'm thinking of a number between 1 and 10. What is it? You have three guesses.")
while numberofGuesses < 3:
numberofGuesses = numberofGuesses +1
randomNumber = (random.randint(1,10))
userInput = 0
userInput = input()
userInput = int(userInput)
if randomNumber > userInput:
print("Too Low! Try again")
if randomNumber < userInput:
print("Too High! Try Again")
if numberofGuesses == 3:
print("Sorry! You lose. The correct number was:",randomNumber)
if randomNumber == userInput:
print("Well Done! Your guess was correct!")`
這段代碼和我提到的那個bug完全不同。
下面的答案是正確的。只需要一個簡單的選擇,'userInput = 0'行就沒有必要了。 – sdasdadas
你也應該看看退出循環時,他們猜對了,否則會讓他們再次猜測。 –