Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 New to Python
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hamid14




PostPosted: Wed Oct 21, 2009 3:48 pm   Post subject: New to Python

Hello, I have just started Python. I am familiar with the print command right now. I know about variables and stuff in python. But I am having trouble with getting string values from the user. How would I do that?

Here's what the code looks like:

>>>name = str
>>>name = input("What is your name")

and then an error comes up when I type in a string value like abcd.
Sponsor
Sponsor
Sponsor
sponsor
SNIPERDUDE




PostPosted: Wed Oct 21, 2009 3:55 pm   Post subject: RE:New to Python

This book helped me learn:
http://www.ibiblio.org/g2swap/byteofpython/files/120/byteofpython_120.pdf
hamid14




PostPosted: Wed Oct 21, 2009 4:03 pm   Post subject: Re: New to Python

nevermind, i figured out.
DtY




PostPosted: Wed Oct 21, 2009 4:05 pm   Post subject: RE:New to Python

Definitely check out that book, but as a quick answer;

I get the impression you already know another language (I'm going to guess Turing)?
Python variables are not bound to a single type, in Turing for example if you want a variable to hold a string you tell it

Turing:
var mystring:String

(Though I probably messed up the syntax, the idea is that mystring now holds a string, and can't hold anything else)

In python though, you create a variable, and that is it, you can put anything you want in it, for example;
Python:
myvariable = "hello, World!" #Holding a string
myvariable = 5 #Now holding an integer
myvariable = 5.5 #Now holding a floating point (real) number

You don't even need to say that you are creating a variable in python, it's implicit.

Now, the line
Python:
name = str

Is perfectly valid, but it does not say that name should hold a string, it literally says name is str, which is the string constructor in python (you can use it to make other types strings), so that is not an error, but it is not right.

The next line the problem is input(), it's a really bad function, but it's good for learning. Anything you type into it is evaluated as a python expression. If you type in "5", you get the integer 5, if you type in "abcd" you get an error because "abcd" (without quotes) means nothing in python (unless you have a variable named "abcd").

To get a string of input, use raw_input().
Using it is the same as input(), but whatever is entered by the user is returned to your script as a string (it's unprocessed, raw)
btiffin




PostPosted: Thu Oct 22, 2009 10:18 pm   Post subject: RE:New to Python

Just a little backfill for anyone reading.
code:
>>> name = input("prompt: ")

is the same as
code:
>>> name = eval(raw_input("prompt: "))

So, with the eval, input() gets weird fast, and is a fair to huge 'evaluation of arbitrary code' security issue.

A short answer to the original problem would be
Quote:
use name = raw_input() instead


Cheers
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: