Reputation: 21
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
Reputation: 126
You need to compare something to your guess_number.
while guess_number != random_number:
Upvotes: 0
Reputation: 1432
A few things:
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
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