Reputation: 3
I'm working on a high/low number guessing game, I'm running into two issues.
import random
high_number = int(input('Enter your high number!\n'))
low_number = int(input('Enter your low number!\n'))
if low_number >= high_number:
print('Your low number must be less than your high number!')
else:
random_number = random.randint(low_number, high_number)
user_guess = int(input(f'Guess a number between {low_number} and {high_number}\n'))
while user_guess != random_number:
if user_guess > random_number:
print('Guess too high, guess another number!')
elif user_guess < random_number:
print('Guess too low, guess another number!')
if user_guess == random_number:
print('You guessed it right!')
Upvotes: 0
Views: 643
Reputation: 67
Like the other answer said, you must use a loop to ensure that a valid low number is given. I implemented this without while True simply because I do not like while True (in my opinion it makes your code less readable and therefore harder to understand). I'm not sure why you stopped updating the variable user_guess in your while loop, how is it ever supposed to end? Additionally, the last if statement is not needed: if the while loop finishes, then user_guess = random_number. This should work
import random
high_number = int(input('Enter your high number!\n'))
low_number = int(input('Enter your low number!\n'))
while low_number >= high_number:
print('Your low number must be less than your high number!')
low_number = int(input('Enter your low number!\n'))
random_number = random.randint(low_number, high_number)
user_guess = int(input(f'Guess a number between {low_number} and {high_number}\n'))
while user_guess != random_number:
if user_guess > random_number:
user_guess = int(input('Guess too high, guess another number!\n'))
elif user_guess < random_number:
user_guess = int(input('Guess too low, guess another number!\n'))
print('You guessed it right!')
Upvotes: 0
Reputation: 54743
You need to think about how things flow, from top to bottom. If they enter the numbers in the wrong order, you print the error, but you keep going into the game without asking for a guess. And your loop just prints forever, without asking for another guess.
This basically works. Note the philosophy of the "ask for info" loops. Do while True
, then if the input is OK, you break from the loop. Otherwise, print an error and the loop will cycle again.
import random
while True:
high_number = int(input('Enter your high number!\n'))
low_number = int(input('Enter your low number!\n'))
if low_number < high_number:
break
print('Your low number must be less than your high number!')
random_number = random.randint(low_number, high_number)
while True:
user_guess = int(input(f'Guess a number between {low_number} and {high_number}\n'))
if user_guess == random_number:
print('You guessed it right!')
break
if user_guess > random_number:
print('Guess too high, guess another number!')
elif user_guess < random_number:
print('Guess too low, guess another number!')
Upvotes: 1