Python Forum

Full Version: can't figure out problem with number guess
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import random 

randomNumber = random.randint(1, 5)

while True: 
  input_value = input("Please guess a number between 1 and 5: ")
  try:
    guess = int(input_value)
  except ValueError: 
    print("{guess} is not a number, please enter a number!".format(guess=input_value))
    continue 
  else:
    break

  if guess < 1:
    print("It has to be a number between 1 and 5.")
    continue
  else:
    break

  if guess > 5:
    print("It has to be a number between 1 and 5.")
    continue
  else:
    break

if guess == (1, 5):
  if guess == randomNumber:
    print("Congrats! {guess} was the correct number!".format(guess=input_value))
  else:
    print("Sorry, {guess} was not the correct number.".format(guess=input_value))
Seems to automatically skip over guess < 1 and guess > 5 after the first break?
import random 

randomNumber = random.randint(1, 5)

while True: 
  input_value = input("Please guess a number between 1 and 5: ")
  try:
    guess = int(input_value)
  except ValueError: 
    print("{guess} is not a number, please enter a number!".format(guess=input_value))
    continue 
  else:
    break

while True:
  input_value = input("Please guess a number between 1 and 5: ")
  guess = int(input_value)
  if guess < 1:
    print("It has to be a number between 1 and 5.")
    continue
  else:
    break

while True:
  input_value = input("Please guess a number between 1 and 5: ")
  guess = int(input_value)
  if guess > 5:
    print("It has to be a number between 1 and 5.")
    continue
  else:
    break

if guess == (1, 5):
  if guess == randomNumber:
    print("Congrats! {guess} was the correct number!".format(guess=input_value))
  else:
    print("Sorry, {guess} was not the correct number.".format(guess=input_value))
After switching stuff around, it now runs without any errors. The only problem is that after giving an input that's higher than 6 or lower than 1 it automatically asks to guess a number between 1 and 5 again without the included message. When you do give a guess that is between 1 and 5, the code automatically ends.
You've written the entry loop 3 times (why?) Don't you expect that it will run 3 times?
(Oct-29-2018, 10:29 AM)Larz60+ Wrote: [ -> ]You've written the entry loop 3 times (why?) Don't you expect that it will run 3 times?

I guess I wanted it to loop back to the original question after every attempt that didn't work? I'm fairly new at Python so I don't completely understand a lot of things.
Galoxys Wrote:I guess I wanted it to loop back to the original question after every attempt that didn't work?
You do it all in one loop.
Can also use !=,so loop run as long random_number is not guessed.
When random_number is guessed it will break out of loop and can print result.
Indentation in Python is 4-space.
import random

random_number = random.randint(1, 5)
tries, guess = 0, 0
while guess != random_number:
   guess = int(input("Take a guess: "))
   if guess > random_number:
       print("Lower...")
   elif guess < random_number:
       print("Higher...")
   tries += 1

print(f'You guessed it! The number was {random_number} in {tries} tries')
Output:
λ python guess_numb.py Take a guess: 4 Lower... Take a guess: 3 Lower... Take a guess: 2 You guessed it! The number was 2 in 3 tries