Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guessing Game Bug?

Tags:

python

I have the following code written in Python as a project. I have taken Computer Science as a GCSE. I am quite new to it, so do not know how to fix this bug. When I type in my first guess, e.g '5', it will say Too High. Then once the game is complete, it will say the answer is 7. It is a bug that I have constantly tried to solve but could not accomplish. Here is the code I have used:

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!")`

This code works perfectly apart from the one bug I mentioned.

like image 996
user2917305 Avatar asked Jan 26 '26 01:01

user2917305


1 Answers

You are reselecting a random number each time you make a guess. Instead I believe you would like to have the program pick a number first, then enter your while loop.

randomNumber = (random.randint(1,10)) 

while numberofGuesses < 3: 

    numberofGuesses = numberofGuesses +1 

    userInput = 0

    userInput = input () 

    userInput = int(userInput) 

    if randomNumber > userInput: 

        print("Too Low! Try again")

    if randomNumber < userInput:

        print("Too High! Try Again")
like image 92
Farmer Joe Avatar answered Jan 27 '26 14:01

Farmer Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!