[Tutor] Issue w/ while loops
Danny Yoo
dyoo at hashcollision.org
Fri Nov 22 20:54:25 CET 2013
> 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.")
>
Here's an example where naming the while loop might be helpful in making
the code easier to understand. Or not. :P Let's see what this might look
like. First, let's take the while loop and make it a function:
#########################################################
def GiveMeAGoodName():
while True:
hours_worked = input("How many hours did you work today? ")
try:
hours_worked = float(hours_worked)
break
except ValueError:
print ("Invalid input")
return hours_worked
print("TIME TRACKING")
hours_worked = GiveMeAGoodName()
if hours_worked < 24:
print("You must be a human.")
else:
print("You must be a cyborg.")
#########################################################
Here, the function-extracting is a little more complex, because there's an
implicit passing of data from one part of the program to the other. The
loop continues to run till hours_worked is a good float, after which the
rest of the program uses that float. So that's why the "GiveMeAGoodName"
returns something.
We can look at GiveMeAGoodName(): it's tryingt to get the number of hours
worked. Let's call it "AskForHoursWorked".
###########################################################
def AskForHoursWorked():
while True:
hours_worked = input("How many hours did you work today? ")
try:
hours_worked = float(hours_worked)
break
except ValueError:
print ("Invalid input")
return hours_worked
print("TIME TRACKING")
hours_worked = AskForHoursWorked()
if hours_worked < 24:
print("You must be a human.")
else:
print("You must be a cyborg.")
#########################################################
If we have a better feeling for how control flow interacts with functions,
we might simplify the lines in AskForHoursWorked() a little bit. Here's
one restatement of that function that does the same thing:
#########################################################
def AskForHoursWorked():
while True:
hours_worked = input("How many hours did you work today? ")
try:
return float(hours_worked)
except ValueError:
print ("Invalid input")
#########################################################
I'd argue that this is a little clearer because, in this variation,
hours_worked is now definitely just a string throughout the program's run.
It doesn't waffle between being a string and being a number. The function
itself is a little shorter because we can do a "return" to get out of the
function, rather than do the "assign the value, break, then return" that we
had in the original code.
Best of wishes!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131122/e0fa2b1c/attachment-0001.html>
More information about the Tutor
mailing list