Reputation: 1
So i'm working on a guessing number game and one of the requirements is for the "range" of numbers to be updated so that the user with have an easier time with their guess'. I created a new input for when the number is either too high or too low. However, i'm not very good with loops and I can't figure out why my loop is repeating only once without my pasting the code over and over again. Can anyone help so the elif statements will repeat until the number is correct?
This is my code thus far...
import random
random_number = random.randint(1,100)
tries = 0
while tries < 800:
guess = input("Guess a random number between 1 and 100.")
tries += 1
try:
guess_num = int(guess)
except:
print("That's not a whole number!")
break
if not guess_num > 0 or not guess_num < 101:
print("That number is not between 1 and 100.")
break
elif guess_num == random_number:
print("Congratulations! You are correct!")
print("It took you {} tries.".format(tries))
break
elif guess_num > random_number:
print("Sorry that number is too high.")
guess_high = input("Guess a number between 0 and {} .".format(guess_num))
elif guess_num < random_number:
print("Sorry that number is too low.")
guess_low = input("Guess a number between {} and 100 .".format(guess_num))
else:
print("Sorry, but my number was {}".format(random_number))
print("You are out of tries. Better luck next time.")
Upvotes: 0
Views: 3696
Reputation: 323
Here is how I would do it. Not sure if it works but the approach would be:
import random
random_number = random.randint(1,100)
max_tries = 100
tries = 0
left = 0
right = 100
found = False
def get_number():
try:
guess = int(input("Guss a random number between {} and {}".format(left, right)))
if guess > right or guess < left:
print("That number is not between {} and {}".format(left, right))
return guess
except:
print("That's not a whole number!")
get_number()
def update_range(guess):
if guess >= left:
left = guess
else:
right = guess
while tries <= max_tries and not found:
guess = get_number()
tries=+1
if guess == random_number:
print("Congratulations! You are correct!")
print("It took you {} tries.".format(tries))
found = True
else:
update_range(guess)
if not found:
print("Sorry, but my number was {}".format(random_number))
print("You are out of tries. Better luck next time.")
As you can see I am using helper functions in order to reduce the number of if/else structures.
Also updating the range is necessary. And thanks to parametrized strings I can tell the user what's the range.
I also set a flag to know whether the user found the number or not at the end of the game
Upvotes: 0
Reputation: 111
For which case does your loop only repeats once?
The loop would break (since you have a break statement in line 15 under the "if not guess_num > 0 or not guess_num < 101:") if the input guess is less than 0 or greater than 100.
If the user input an incorrect guess between 1 and 100, then the code does loop and hence the user is asked to input another number.
Although it does loop, the loop is not updating the range. What you can do is set range_min and range_max as variables and change these variables according to if the guess is too low or too high. See the following code:
import random
random_number = random.randint(1,100)
tries = 0
range_min = 0
range_max = 100
while tries < 800:
guess = input("Guess a random number between " + str(range_min +1) + " and " + str(range_max))
tries += 1
try:
guess_num = int(guess)
except:
print("That's not a whole number!")
break
if not guess_num > range_min or not guess_num < (range_max+1):
print("That number is not between str(range_min +1) + " and " + str(range_max)")
break
elif guess_num == random_number:
print("Congratulations! You are correct!")
print("It took you {} tries.".format(tries))
break
elif guess_num > random_number:
print("Sorry that number is too high.")
guess_high = input("Guess a number between 0 and {} .".format(guess_num))
range_max = guess_num
elif guess_num < random_number:
print("Sorry that number is too low.")
guess_low = input("Guess a number between {} and 100 .".format(guess_num))
range_min = guess_num
else:
print("Sorry, but my number was {}".format(random_number))
print("You are out of tries. Better luck next time.")
Upvotes: 0
Reputation: 2153
Here is how to update the ranges:
import random
random_number = random.randint(1,100)
tries = 0
low = 0
high = 100
while tries < 800:
if(tries == 0):
guess = input("Guess a random number between {} and {}.".format(low, high))
tries += 1
try:
guess_num = int(guess)
except:
print("That's not a whole number!")
break
if guess_num < low or guess_num > high:
print("That number is not between {} and {}.".format(low, high))
break
elif guess_num == random_number:
print("Congratulations! You are correct!")
print("It took you {} tries.".format(tries))
break
elif guess_num > random_number:
print("Sorry that number is too high.")
high = guess_num
guess = input("Guess a number between {} and {} .".format(low, high))
elif guess_num < random_number:
print("Sorry that number is too low.")
low = guess_num
guess = input("Guess a number between {} and {} .".format(low, high))
else:
print("Sorry, but my number was {}".format(random_number))
print("You are out of tries. Better luck next time.")
Upvotes: 0
Reputation: 2691
Do not use break
, as they terminate the while loop. See python language reference.
You should ask only once for guess_num
. Remove the two input
when the guess is too high or too low (BTW variable name guess
was incorrect).
The last condition (out of tries) should be outside the while
loop, since it will be executed when the number of tries is over 800.
Hint. A user can play this game with the equivlent of a binary search (start with 50, then guess 25 or 75 depending if higher/lower, etc). You should allow about 8 tries (not 800).
Upvotes: 0
Reputation: 35720
import random
random_number = random.randint(1,100)
tries = 0
guess = input("Guess a random number between 1 and 100.")
while tries < 800:
tries += 1
try:
guess_num = int(guess)
except:
print("That's not a whole number!")
break
if not guess_num > 0 or not guess_num < 101:
print("That number is not between 1 and 100.")
break
elif guess_num == random_number:
print("Congratulations! You are correct!")
print("It took you {} tries.".format(tries))
break
elif guess_num > random_number:
print("Sorry that number is too high.")
guess = input("Guess a number between 0 and {} .".format(guess_num))
elif guess_num < random_number:
print("Sorry that number is too low.")
guess = input("Guess a number between {} and 100 .".format(guess_num))
else:
print("Sorry, but my number was {}".format(random_number))
print("You are out of tries. Better luck next time.")
Upvotes: 1