我有以下用 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!")`
除了我提到的一个错误之外,此代码可以完美运行。