Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
repeating a user_input
#11
Don't do this
while user_input != RNumber:
    # To get here, user_input must be greater than or less than RNumber.  It cannot equal RNumber
    if user_input > RNumber:
        user_input = int(input("choose a lower number: \n"))   # <-- one of these will run
    elif user_input < RNumber:                                 #   |
        user_input = int(input("choose a higher number: \n"))  # <-
    elif user_input == RNumber:                                 
        print("the number you chose is correct (", RNumber, ")")  # <- This will never run
You could do this (but you shouldn't)
while user_input != RNumber:
    if user_input > RNumber:
        user_input = int(input("choose a lower number: \n"))
    elif user_input < RNumber:
        user_input = int(input("choose a higher number: \n"))
    # Make this if statement look at the new value of user_input.  Still doesn't
    # work if the first guess is correct,
    if user_input == RNumber:
        print("the number you chose is correct (", RNumber, ")")
You should do this:
while user_input != RNumber:
    if user_input > RNumber:
        user_input = int(input("choose a lower number: \n"))
    elif user_input < RNumber:
        user_input = int(input("choose a higher number: \n"))
# Only way to get here is to guess correctly.
print("the number you chose is correct (", RNumber, ")")
The only way to exit the loop is to guess the correct number. If the loop is done, the user guessed the number. No need for any check.

if statements are bad. They make code difficult to understand and they make programs run slow. But if statements are a necessary evil. Use them when needed. Avoid using when you don't.
Reply


Messages In This Thread
repeating a user_input - by astral_travel - Oct-25-2022, 03:49 PM
RE: repeating a user_input - by rob101 - Oct-25-2022, 03:55 PM
RE: repeating a user_input - by astral_travel - Oct-25-2022, 04:25 PM
RE: repeating a user_input - by deanhystad - Oct-25-2022, 04:25 PM
RE: repeating a user_input - by astral_travel - Oct-25-2022, 05:23 PM
RE: repeating a user_input - by astral_travel - Oct-25-2022, 04:30 PM
RE: repeating a user_input - by rob101 - Oct-25-2022, 04:40 PM
RE: repeating a user_input - by astral_travel - Oct-25-2022, 04:41 PM
RE: repeating a user_input - by rob101 - Oct-25-2022, 04:54 PM
RE: repeating a user_input - by astral_travel - Oct-25-2022, 05:05 PM
RE: repeating a user_input - by rob101 - Oct-25-2022, 05:10 PM
RE: repeating a user_input - by deanhystad - Oct-25-2022, 05:11 PM
RE: repeating a user_input - by rob101 - Oct-25-2022, 05:17 PM
RE: repeating a user_input - by deanhystad - Oct-25-2022, 09:25 PM
RE: repeating a user_input - by rob101 - Oct-26-2022, 07:55 AM
RE: repeating a user_input - by perfringo - Oct-26-2022, 08:41 AM
RE: repeating a user_input - by Pedroski55 - Oct-26-2022, 09:17 AM
RE: repeating a user_input - by astral_travel - Oct-26-2022, 04:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 816 Dec-12-2023, 09:09 AM
Last Post: buran
  if else repeating Frankduc 12 2,729 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  factorial, repeating Aldiyar 4 2,896 Sep-01-2020, 05:22 PM
Last Post: DPaul
  Repeating equations Tbot100 2 3,340 May-29-2019, 02:38 AM
Last Post: heiner55
  repeating for loop Kaldesyvon 5 3,963 Dec-06-2018, 08:00 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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