[Tutor] Issue w/ while loops

Marc Tompkins marc.tompkins at gmail.com
Sat Nov 23 23:46:56 CET 2013


On Sat, Nov 23, 2013 at 8:30 AM, Rafael Knuth <rafael.knuth at gmail.com>wrote:

> Marc,
>
> great feedback - thank you very much!
> I will bear that in mind for the future.
>
> I modified my program as you suggested, but I received a runtime
> error; I tried to fix that but unfortunately I didn't succeed.
> The modified program executes well only when the user input has the
> correct format (integer or float) and it breaks when the user enters a
> string (at that point the program should print ("Invalid input") and
> loop back to its beginning).
> Can you please advise how I should change the program?
> Thank you so much!
> Raf
>
> Here's the original program:
>
> 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.")
>
> Here's the modified version:
>
> print("TIME TRACKING")
>
> hours_worked = "Invalid input"
> while hours_worked == "Invalid input":
>     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.")
>
> And here's the error message I get:
>
> >>>
> TIME TRACKING
> How many hours did you work today? very long hours
> Invalid input
> Traceback (most recent call last):
>   File "C:\Users\Rafael_Knuth\Desktop\Rookies At
> Work\Python\TimeReckord.py", line 12, in <module>
>     if hours_worked < 24:
> TypeError: unorderable types: str() < int()
> >>>
>

The TypeError is basically just telling you that there's no meaningful way
to compare the string "very long hours" with the integer 24.  I should have
recommended an exit condition more along the lines of checking whether
hours_worked was still a string, like so:
while isinstance(hours_worked, str)

What I actually had in mind was to put the assignment to hours_worked
inside the try/except - initialize it to a value that will keep you inside
the loop, and only change its value to something that will escape the loop
if the user enters a valid value.  Like so:

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

Note that I changed "input" to raw_input; I'm still using Python 2.7.  If
you're using Python 3, please disregard.

In any case, the objective I was after was to make the "break"
unnecessary.  "Break"ing out of a loop should only happen in unusual cases;
it shouldn't be the primary flow control.  (Not because it's necessarily
less efficient, or computationally wrong - but because a future maintainer
will have to drill down into the loop to figure out what's happening.)  If
your IDE supports collapsing indented sections, it should be possible to
understand the code flow without fully expanding it - or at least that's
what I strive for.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131123/53440394/attachment.html>


More information about the Tutor mailing list