can't figure out problem with number guess - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: can't figure out problem with number guess (/thread-13725.html) |
can't figure out problem with number guess - Galoxys - Oct-29-2018 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? RE: can't figure out problem with number guess - Galoxys - Oct-29-2018 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. RE: can't figure out problem with number guess - Larz60+ - Oct-29-2018 You've written the entry loop 3 times (why?) Don't you expect that it will run 3 times? RE: can't figure out problem with number guess - Galoxys - Oct-29-2018 (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. RE: can't figure out problem with number guess - snippsat - Oct-29-2018 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')
|