我正在尝试做一个简单的猜测数字游戏,计算机会随机选择一个随机数字,然后用户输入 1 到 10 之间的正确数字,猜测限制为 6。
我是Python初学者,需要一些帮助来解决这个简单的问题。我知道你们很多人都知道,并且很容易看到这里的问题,所以请帮助我:)
谢谢你!! :)
import random
guess = ''
noOfGuess = 0
guessLimit = 6
randomNum = random.randint(1, 10)
print("I am thinking of a random number between 1 and 10.")
while noOfGuess <= guessLimit or guess != randomNum:
guess = int(input("Take a guess: "))
noOfGuess += 1
if guess < randomNum:
print("Too low. Try again.")
elif guess > randomNum:
print("Too high. Try again.")
else:
break
if guess == randomNum:
print(f"Good job! You guessed my number in {noOfGuess} guesses.")
else:
print(f"Nope. I am thinking of {randomNum}.")
在“while noOfGuess”行中<= guessLimit or guess != randomNum:' , instead of or it should be and . Also since you start noOfGuess in 0 no need for <= , can make it <. So you can change it to 'while noOfGuess < guessLimit and guess != randomNum:' and try.