RetroSteve0
RetroSteve0

Reputation: 21

What am I doing wrong? My code tells me the guess is incorrect, when it is correct

I am trying to make a small program that will ask someone to guess a number that is being picked. When the number is correctly guessed, the program should end. When the number is incorrectly guessed, it should repeatedly ask the user to try again until the correct answer is guessed. I intentionally set the random value to always be 1 just for testing purposes.

import random

random_number = random.randint(1, 1)
guess_number = int(input('I have picked a random number. Can you guess what it is? '))
while guess_number != True:
    int(input('I am sorry. That is not correct. Please try again. '))
else:
    print('That is correct!')

I know whatever I'm doing wrong is extremely simple and I'll likely kick myself when I find the answer, but for the life of me I can not figure it out right now.

Upvotes: 1

Views: 83

Answers (3)

J L
J L

Reputation: 126

You need to compare something to your guess_number.

while guess_number != random_number:

Upvotes: 0

mario_sunny
mario_sunny

Reputation: 1432

A few things:

  1. You're generating a random number between 1 and 1, which is not very random at all. It should be between two different numbers.
  2. Your looping condition is not correct. You should be comparing the randomly generated number to the guessed number.
  3. You're not setting the second input equal to anything.

Here's a working solution that generates numbers between 1 and 10:

import random

random_number = random.randint(1, 10)
guess_number = -1
while True:
    guess_number = int(input('I have picked a random number. Can you guess what it is? '))

    if guess_number == random_number:
        print('That is correct!')
        break

    print('I am sorry. That is not correct. Please try again. ')

Upvotes: 0

eumiro
eumiro

Reputation: 213015

You have your random_number, but you don't compare it to your guess_number anywhere.

You only compare the guess_number (an integer) to True (a boolean), which is obviously false.

Please compare your code with the following one and ask if there is anything unclear:

import random

random_number = random.randint(1, 1)
guess_number = int(input('I have picked a random number. Can you guess what it is? '))
while guess_number != random_number:
    guess_number = int(input('I am sorry. That is not correct. Please try again. '))
print('That is correct!')

Also, random.randint(1, 1) always returns 1. You probably want to use different numbers.

Upvotes: 1

Related Questions