Accepting text input

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 12 01:39:11 EDT 2008


En Mon, 12 May 2008 01:54:28 -0300, Collin <collinyeung at shaw.ca> escribió:

> Collin wrote:
>> I'm pretty new to Python, but this has really bugged me. I can't find a
>> way around it.
>>
>>
>> The problem is that, when I use raw_input("sajfasjdf") whatever, or
>> input("dsjfadsjfa"), you can only have numerical values as answers.
>>
>> Any help would be appreciated. Thanks.
>
>
> Oh, wow. I feel so stupid. Please disregard this message. <_<

No need to apologize...

> I read the error message just now a bit more carefully, and I tried
> something. I tried defining "yes" as some random numerical value. Then
> when I did:
> (example code)
>
> yes = 123123983 #some number
> test = input("Test test test ")
> if test == yes:
> 	print "It worked."
> else:
> 	print "failed"
>
> (example code off)

The usual way for Python<3.0 is:

answer = raw_input("Test test test ").lower()
if answer == "yes":
     ...

The input() function evaluates user input as an expression: if he types 2+5 the input() function returns the integer 7. I would never use input() in a program - it's way too unsafe; use always raw_input instead.

-- 
Gabriel Genellina




More information about the Python-list mailing list