raw_input
Syntax
result |
= |
raw_input |
( |
'question?' |
) |
About raw_input
raw_input allows you to get text from the user for later use in your program. In order to use raw_input you need to combine it with variables.
The sample code is taken from the video. Try and add a few more questions and a few more calculations to ensure you understand it.
name = raw_input("What is your name?")
hobby = raw_input("What is your hobby?")
age = int( raw_input("how old are you?") )
print "hello", name
print "you like", hobby
print "your age is", age
print "your age doubled is", age * 2
Note – If you need a number then you must use int() as shown on line 3.
Key term
Casting – the process of converting a variable from one data type to another based on the casting rules of the language.
Helpful links
http://python4kids.wordpress.com/2010/07/01/interacting-with-raw_input/
http://en.wikibooks.org/wiki/Non-Programmer’s_Tutorial_for_Python_2.6/Who_Goes_There%3F
http://www.wellho.net/resources/ex.php4?item=y102/yraw – NOTE this link shows the difference between input and raw_input
http://learnpythonthehardway.org/book/ex14.html
Test your skill
1. Create a program which will calculate the area of a circle using the formula 2 * PI * R. The partial code is shown below. To get this to work you must cast the text to a string!
radius = ??????????? PI = 3.14 print "the area is", 2 * PI * radius
2. Write a short program which will take any question entered by the user and will then print “do you know” in front of it.











