[Tutor] Simple User Entry Verification

Alan Gauld alan.gauld at btinternet.com
Sun Mar 15 01:35:20 CET 2009

"Ian Egland" <echolynx at gmail.com> wrote

> Newbie here who can't figure out why this doesn't work:

It worked perfectly for me.

But then, maybe I expected it to do something different to
what you expected? When posting please tell us what you
expected to happen, what actually happened plus any error
messages (in full) (It also helps if you tell us what OS and
what version of Python. I'd guess from your print statements
that you are using v3 here?)

That way we can align our answers to your expectation...

> start = input("Please enter the starting number.\n>")
> print(type(start))
> while type(start)!=float:
>    start = input("Sorry, that number was invalid.\nPlease enter the
> starting number.\n>")
>    print(type(start))

The key thing to note here is that input() in Python v3 always returns
a string,  you need to convert it to a different type. In your case 
probably want to try converting to a float:

start = float(input("Please enter the starting number.\n>"))

> while type(start)!=float:

Now instead of using a while loop to test the type you can expect
an exception if the type is not a float. But that's a topic for 
another time!

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list