Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!! 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}.")

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading