Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Tutorial >> 'raw_input' and user input

Holiverh's izHawt Python....

Tutorial >> 'raw_input' and user input

Postby Holiverh » Fri Aug 28, 2009 2:06 am

For this tutorial you will need the interactive Python shell IDLE.
Also i recommend you read my previous tutorial "'print', output and variables" if you have not done so already.


If you have read my last tutorial you will know outputting to the screen is rather simple, and so is the process of creating and associating[1] data with variables. This time i will try to teach you another really useful technique. Some computer programs run completely independant of humans interaction, but many require a humans to give it some data to work with.

So here's the syntax:
Code: Select all
raw_input()
Yes, it's that simple. A quick side note: There is another function which goes by the name of 'input()', we don't need to worry about him now. Raw_input will wait for a user to enter some data and hit return. But as the code is at the moment we would lose that data as soon as it's enter, so we need to store it in a varible.
Code: Select all
x =  raw_input()
Yes, it's stil that simple! :D This exspression in Python will take whatever input the user provides and --in our case-- store it in a variable called 'x'. We now have the data saved.

If you have tried this peice of code in the Python shell, you'll notice that once you call the 'raw_input()' function it skip down to the next line and...waits. This is fine for us, as we know what it's waiting for, but what if we were a first time user of the program, we wouldn't know what to do. Well raw_input takes an argument that takes care of all out woes. Raw_input takes a string[2] argument, this argument is then turned into a prompt which is out putted to the user.
Code: Select all
x = raw_input("What is your name?")
This will out put 'What is your name?' before waiting for the users input. You would get the same effect with:
Code: Select all
print "What is your name?"
x = raw_input()


Wow that really is simple!!! :O But before you can say you're a master of getting user input you'll have to understand the next step. When you call 'raw_input()' the data the user enters will automatically returned as a string. We introduce a knew function to test this:
Code: Select all
x = raw_input("Please enter some jibberish:")
print type(x)
This will return: <type 'str'>. This clearly shows that the object 'x' is a 'str' or string object. Now if you can't imediatly see the potential problems with this, try to run this code in the interpreter:
Code: Select all
num = raw_input("Enter a number:")
print num + 10
Now presuming you followed the prompt; you enter a number. Some of you may have thought, 'What's the point of this? I'm going to get a nice fat number at the end of this!'. But instead you got a 'TypeError' saying str objects and int objects cannot be concatenated. This is because Python doesn't like it when you mix datatypes. But don't let this worry you, you can easily over come this with a simple string to integer conversion:
Code: Select all
foo = raw_input("Please enter a number:")
print int(foo) + 10
This new function: 'int()' converts the contents of the variable 'foo' to a integer, thus allowing us to preform mathimatical operation on it.

Short note before i end this tutorial. If someone enters a string when you want an integer, and then you try to convert the string to an integer you will raise a ValueError. Don't worry this can be overcome using 'exception catching' which we will get to in a later tutorial.

If there is something you don't fully understand or would like clarification, feel free to post a reply to this thread, or alternately contact me through PM or even Email if your incapable of using the PM system! :P :lol:

Footnotes:
[1]: Important note to remember if you choose to look deeper into Python. Variables in Pythons are simply references to the objects which are stored in the memory. Simple put; they are just labels for peices of data, the the actual data itself.
[2]: A string is a data type that is identifyed by being enclosed in inverted commas or speach marks; ""


-------------------------------------------------------------------------------------------------------------
Image

Python tutorial: 'input' and 'raw_input' by Oliver Haddock is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 UK: England & Wales License. Permissions beyond the scope of this license may be available at ottajay@googlemail.com.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: Tutorial >> 'raw_input' and user input

Postby Dink » Fri Aug 28, 2009 7:34 pm

Holiverh wrote:This new function: 'int()' converts the contents of the variable 'foo' to a integer, thus allowing us to preform mathimatical operation on it.


int just manipulates the variable for that use temporarily right? I.e. afterwards the variable foo will still be a string correct?

Holiverh wrote:Short note before i end this tutorial. If someone enters a string when you want an integer, and then you try to convert the string to an integer you will raise a ValueError. Don't worry this can be overcome using 'exception catching' which we will get to in a later tutorial.


Don't you just explain how to do that though with the int function?
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tutorial >> 'raw_input' and user input

Postby Holiverh » Fri Aug 28, 2009 7:45 pm

Dink wrote:
Holiverh wrote:This new function: 'int()' converts the contents of the variable 'foo' to a integer, thus allowing us to preform mathimatical operation on it.


int just manipulates the variable for that use temporarily right? I.e. afterwards the variable foo will still be a string correct?
Yes. the int() function will 'temporarily' convert the varible to a integer. To permanently change it you could do:
Code: Select all
x = raw_input("Number: ")
x = int(x)


Dink wrote:
"Holiverh wrote:Short note before i end this tutorial. If someone enters a string when you want an integer, and then you try to convert the string to an integer you will raise a ValueError. Don't worry this can be overcome using 'exception catching' which we will get to in a later tutorial.


Don't you just explain how to do that though with the int function?
SOrry if this part was not clear. No, this is not the same as the int function. What i mean was: If you were expecting a number, and you got a number you convert it from it's default string format to an integer with int().
However if you were expecting a number and you got a string of characters; when you try to convert to a integer you will raise a ValueError, because strings can't be converted to numbers. :)
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tutorial >> 'raw_input' and user input

Postby Dink » Sat Aug 29, 2009 10:39 am

It has been added to the site; http://www.dinksoftware.com/python_02.php :ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia


Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred