Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
repeating a user_input
#14
rob101's example uses an expression in the while statement "while user_input != RNumber". The expression "input != RNumber" evaluates to the Python object True or False. If the result of the expression is True, the while loop continues to run. If the result is False, the loop stops.

My example uses "while True:". This skips evaluating an expression to get True or False, but otherwise works exactly like rob101's example. If the result is True, the while loop continues to run. If the result is False, the loop stops. True will always be True, so the loop runs forever.

Actually, that's not really true. My while loop loops forever because True is always truthful. Things can be "truthful" without being the object True. In Python, 1 is truthful and 0 is not. A list containing values is truthful, but an empty list is not. Python lets you use things other than True and False as the conditionals for "if" or "while" statements.

Most things in Python are truthful. It is easier to list the things that are not truthful.
1. False is obviously not truthful.
2. Comparisons and Boolean expressions that evaluate to False are not truthful.
3. None is not truthful.
4. The number zero is not truthful.
5. Empty collections are not truthful (lists, tuples, sets, dictionaries, bytearrays).
6. Zero length strings and bytes are not truthful.
7. Classes that override the __bool__() method to return False are not truthful.

So, back to the question about "while True:". Why make a loop that loops forever? Won't the program just hang?

"Forever" loops are actually very common in Python. Using a forever loop almost always indicates that the programmer wants to loop, but want's more control over how loop exits. Look at the body of a forever loop and you will find a "break" statement or a "return statement" (if the loop is inside a function). These exit the loop instead of testing the condition at the top.

My previous example:
import random
 
rnumber = random.randint(0, 10)
prompt = "Enter a number from 0 to 10: "
while True:  # I cannot check if it is time to exit the loop because no value is entered yet
    # I only want to do the str->int conversion in one place.  I might wrap an exception handler
    # around the conversion to prevent crashing the program if a non-numeric string is entered.
    unumber = int(input(prompt))
    if unumber > rnumber:
        prompt = "Guess a lower number: "
    elif unumber < rnumber:
        prompt = "Guess a higher number: "
    else:
        break   # This is where I exit the loop
print("That's it!")
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 811 Dec-12-2023, 09:09 AM
Last Post: buran
  if else repeating Frankduc 12 2,716 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  factorial, repeating Aldiyar 4 2,893 Sep-01-2020, 05:22 PM
Last Post: DPaul
  Repeating equations Tbot100 2 3,337 May-29-2019, 02:38 AM
Last Post: heiner55
  repeating for loop Kaldesyvon 5 3,962 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