Keyboard Input -- Numbers
The "input" command is used to input information from the keyboard and assign it to a variable, as in:
mark = input("What mark did you get? ")
Optionally, you can print a statement and then wait for the keyboard input, as follows:
print "What mark did you get? "
mark = input()
If you were to print the answer by a number, as follows:
print (mark * 2)
...you would get an error, because Python is expecting a string (text) to be entered. To solve this problem, we must cast (convert) the string to an integer, as follows:
print (2 * int(mark))
If we wanted to enter a real number (float), we would cast its value as follows:
print (2 * float(mark))
Here is an example that prompts for and prints your birth year:
print ("What year were you born?")
birthYear = int(input())
print ("You were born in", birthYear)
Assignment
Write a program that prompts for your year of birth and the current year, then calculates and neatly outputs your age.
Save as "018.py".