[Tutor] Input handling?

Dave Angel d at davea.name
Tue Sep 18 06:04:22 CEST 2012


On 09/17/2012 11:11 PM, Scott Yamamoto wrote:
> I've been trying to find possible erros with input(such as NameError or SyntaxError) to handle them with an except clause. however, I've found that hitting enter/return while prompted without inputting creates some kind of problem.
>>>> username = raw_input("Input a username: ")
> Input a username: #hits enter, encounter problem here. Loops forever or something
>
> Using python for ios
>

Somehow you managed to put your other message in its own thread, instead
of adding to this one.

What version of Python are you running?  If it's 2.x, and if you're
using input(), then you're doing a (hazardous) eval on whatever the user
types.  So if the user doesn't type a valid expression, or if that
expression doesn't evaluate to a number, you'll get some exception. 
Clearly, you want raw_input(), which is looking for a string, and
doesn't mind empty strings from the user.

It would be useful (in the future) if you clearly stated what version of
Python you're using, what exactly was included in your script, and what
exception you got, with full traceback.  Much more useful to diagnose
than a vague - "some kind of problem."


-- 

DaveA



More information about the Tutor mailing list