如何解决这个简单的Python问题,我不知道如何解决。有一些错误我无法调试

问题描述 投票:0回答:1

我正在尝试做一个简单的猜测数字游戏,计算机会随机选择一个随机数字,然后用户输入 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}.")
python while-loop python-import
1个回答
0
投票

在“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.

© www.soinside.com 2019 - 2024. All rights reserved.