[Tutor] Issue w/ while loops

Rafael Knuth rafael.knuth at gmail.com
Fri Nov 22 11:11:45 CET 2013


>> I'm only stuck at one point: How do I loop back to the beginning in
>> case the user input is invalid?
>
>
> Look at Peter's example. He set a variable to false when the input was
> wrong. You can check that value in your while loop.

Ok, got you!

print("TIME TRACKING")

while True:
    hours_worked = input("How many hours did you work today? ")
    try:
        hours_worked = float(hours_worked)
        break
    except ValueError:
        print ("Invalid input")
if hours_worked < 24:
    print("You must be a human.")
else:
    print("You must be a cyborg.")

Program works well now, a learned a lot along the way.
Thank you & have a great weekend,

Raf


More information about the Tutor mailing list