Python Forum
Fundamental understanding-problem regards to while-loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fundamental understanding-problem regards to while-loops
#1
Hi!

Following code is correct and executes without any problems:

while True:
    age = input('Enter your age:\n')
    if age.isdecimal():
        break
    print("Please enter a number for your age.")

while True:
    print("Select a new password (letters and numbers only):")
    password = input()
    if password.isalnum():
        break
    print('Passwords can only have letters and numbers.')
But I want to understand why?
I mean, the while-statements says ' while True', it will loop, as I understand it.
Now regards to this example, the program is supposed to break, when the if-statement is evaluating to the boolean value 'True' and loop if it should evaluate to the boolean value 'False'?
Basically, I would get it when the code at line 1 would say 'while not True'.
Has it maybe to do that regards to boolean algebra following is true:
if a = false and b = true, then a -> b is true?

Or why is it the the prrogram is looping in this case, while the input will be evaluated to 'False'?

regards,
Placebo

Update:

Or maybe i do get the while loops fundamentally wrong and it is like following:
When a while-statement is set to True, the program will execute the code as long as all is evaluated to true.
Only when something is evaluated to False, the code-execution will stop and loop at instructional position where it got stuck (have been evaluated to false), until the input will be evaluated to True.
Only then the program will resume and proceed the execution of the code.
Is it like that, in general?

This example also illustrates well me confusion:
https://gyazo.com/4bf66ea00cd4a5d76842dfb2224830fc

why does the program right away assume that everything is true, without any input?
Reply


Messages In This Thread
Fundamental understanding-problem regards to while-loops - by Placebo - Oct-09-2018, 12:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 4,761 Aug-01-2022, 06:08 PM
Last Post: paulyan
  problem with for loops? Darbandiman123 1 2,038 Sep-26-2018, 06:35 PM
Last Post: nilamo
  Need help with understanding for/while loops! WombatHat42 2 2,361 Sep-26-2018, 11:46 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

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