[Tutor] Issue w/ while loops

Rafael Knuth rafael.knuth at gmail.com
Thu Nov 21 14:17:49 CET 2013


Hej there,

@David @Peter @Amit:
Thank you so much - you guys helped me understand my misconceptions
and I learned a couple new things.

On Thu, Nov 21, 2013 at 12:44 PM, Amit Saha <amitsaha.in at gmail.com> wrote:
> On Thu, Nov 21, 2013 at 9:00 PM, Rafael Knuth <rafael.knuth at gmail.com> wrote:
>> Hej there,
>>
>> I want to use a while loop in a program (version used: Python 3.3.0),
>> and I expect it to loop unless the user enters an integer or a
>> floating-point number instead of a string.
>>
>> print("TIME TRACKING")
>> hours_worked = input("How many hours did you work today? ")
>> while hours_worked != str() or int():
>>     hours_worked = input("Can't understand you. Please enter a number!  ")
>>
>> print("you worked " + str(hours_worked) + " hours today.")
>>
>> When I run the program, it keeps looping even if the condition is met.
>> How do I need to modify the program on the 3rd line so that it stops
>> looping when the user enters a floating-point number or an integer?
>
> There are two fundamental mistakes in your program:
>
> 1. The input() function always returns a string. So, there is no way
> to check directly whether the user has entered a number or a string.
> 2.  hours_worked != str() or int() does not do what you want to do. In
> Python, str() creates a new string object and similarly int() creates
> an integer object, 0.

Got you, thank you for the clarification.

>>>> def check_input(user_input):
> ...     try:
> ...             user_input = float(user_input)
> ...     except ValueError:
> ...             return 'Invalid input'
> ...     else:
> ...             return user_input
> ...
>>>> check_input('a')
> 'Invalid input'
>>>> check_input('1.5')
> 1.5
>>>> check_input('1')
> 1.0
>
> The idea above is basically, you convert the input (a string) to a
> float. If the input is a number, 1.5 or 1, the check_input() function
> will return the numeric equivalent. However, if the number is a
> string, it returns invalid input. You could make use of this in your
> program above.
>
> Hope that helps.

It definitely does!
I am completely new to programming, and I am taking a Python course at
Codecademy.
In addition to that, I write tiny, little throw-away programs along
the way in order to get more practice.
The concept of try/except/else was new to me and it's extremely
valuable to know how make use of it.

I'm only stuck at one point: How do I loop back to the beginning in
case the user input is invalid?
I want the program to loop until the user enters a value that is
either a float or an int.
None of my code modifications gave me the desired result.

In case the user input is correct, I can move on and analyze it as I
figured out, for example:

print("TIME TRACKING")
hours_worked = input("How many hours did you work today? ")

try:
    hours_worked = float(hours_worked)
except ValueError:
    print ("Invalid input")
if hours_worked < 24:
    print("You must be a human.")
else:
    print("You must be a cyborg.")

All the best,

Raf


More information about the Tutor mailing list