Accepting text input

Kam-Hung Soh kamhung.soh at gmail.com
Thu May 15 00:37:49 EDT 2008


On Thu, 15 May 2008 12:36:29 +1000, Collin <collinyeung at shaw.ca> wrote:

> Kam-Hung Soh wrote:
>> On Wed, 14 May 2008 11:02:36 +1000, Collin <collinyeung at shaw.ca> wrote:
>>
>>> Gabriel Genellina wrote:
>>>> 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.
>>>>
>>>
>>> If I use it like that, do I have to import anything to have the  
>>> .lower() work? And if I do, what does the .lower() signify?
>>> -- http://mail.python.org/mailman/listinfo/python-list
>>>
>>  You don't need to import any module to use ".lower()"; it is a method  
>> of a string.  raw_input() returns a string, so you can use methods of a  
>> string.
>>  Try the following statement to see what happens:
>> "ABCDE".lower()
>>
>
> So the .lower() string method is just to convert the string to lowercase  
> letters so that you don't have to type a bunch of if - then statements  
> in both cases, I'm assuming?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

That's right.  If you normalize your input to all lower case or upper  
case, you make it easier to process user input.

Regards,

-- 
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>




More information about the Python-list mailing list