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()
- Code: Select all
x = raw_input()
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?")
- 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)
- Code: Select all
num = raw_input("Enter a number:")
print num + 10
- Code: Select all
foo = raw_input("Please enter a number:")
print int(foo) + 10
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!
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; ""
-------------------------------------------------------------------------------------------------------------

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.
