Accepting text input

C

Collin

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.
 
C

Collin

Collin said:
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. <_<

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)

Collin
 
G

Gabriel Genellina

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.
 
C

Collin

Gabriel said:
No need to apologize...


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?
 
K

Kam-Hung Soh

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?

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()
 
C

Collin

Kam-Hung Soh said:
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?
 
J

John Salerno

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?

You can also type:

dir(str)

to get a list of all the methods you can call on a string object. If you see anything interesting, then type:

help(str.<method_name>) # e.g. help(str.split)

to find out how it works. :)
 
K

Kam-Hung Soh

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?

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

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top