Python Forum
can't figure out problem with number guess
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't figure out problem with number guess
#1
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?
Reply
#2
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.
Reply
#3
You've written the entry loop 3 times (why?) Don't you expect that it will run 3 times?
Reply
#4
(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.
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 2,959 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Problem with "Number List" problem on HackerRank Pnerd 5 2,034 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  number accuracy problem? roym 5 1,795 Dec-24-2021, 07:57 AM
Last Post: roym
  Problem : Count the number of Duplicates NeedHelpPython 3 4,280 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Ask again if wrong guess with While. EduPy 4 2,210 Oct-21-2021, 07:46 PM
Last Post: menator01
  I guess it's about print tsavoSG 2 2,075 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,099 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  problem with complex number jodrickcolina 1 2,286 Apr-13-2019, 06:59 PM
Last Post: Yoriz
  Guess a number Ameen 5 13,070 Apr-03-2018, 01:20 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,096 Jan-07-2018, 04:26 AM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020