Python 101 - Lesson 02
Lesson 02 - User input
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 data with variables. This time I will teach you another really useful technique. Some computer programs run completely independent of humans interaction, but many require humans to give it some data to work with.
So here's the syntax:
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 variable.
x = raw_input()
Yes, it's still that simple! This expression 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 tried this piece of code in the Python shell, you'll notice that once you call the 'raw_input()' function it skips 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 our woes. Raw_input takes a string argument, this argument is then turned into a prompt which isthen out- putted to the user.
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 result with:
print "What is your name?"
x = raw_input()
Wow, that really is simple! 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 be returned as a string. We will introduce a new function to test this:
x = raw_input("Please enter some gibberish:")
This will return: <type 'str'>. This clearly shows that the object 'x' is a 'str' or string object. Now if you can't immediately see the potential problems with this, try to run this code in the interpreter:
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 data types. But don't let this worry you, you can easily overcome this with a simple string to integer conversion:
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 perform mathematical operations on it.
Note: 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.
Help for this tutorial is available as always on the forum.
If there is something you don't fully understand or would like clarification, feel free to post a reply in this thread, or alternately contact me through PM or even Email.
: Important note to remember if you choose to look deeper into Python. Variables in Python are simply references to the objects which are stored in the memory. Simply put; they are just labels for pieces of data, not the actual data itself.
: A string is a data type that is identified by being enclosed in inverted commas or speech marks; ""
This Python tutorial 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 email@example.com