How to solve this simple python problem, I don't know how to fix. There's some error I can't debug

Question:

I’m trying to make a simple guess the number game which the computer would randomly pick a random number then the user would input the correct number between 1 and 10 and there’s a guess limit of 6.

I’m a beginner in python, would need some assisst with this simple problem. I know a lot of you know and would easily see the problem here so please help me 🙂

Thank youu!! 🙂

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}.")
Asked By: rain

||

Answers:

In the line ‘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.

Actually you only need ‘while noOfGuess < guessLimit’ since you are checking guess != randomNum inside the while loop.

Answered By: KRG
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.