Input/Output
The keyword input( ) is used to get data from the user.
input_output.py
# input pet_name with 'Enter pet name:' pet_name = input('Enter pet name:') # input age with 'Enter pet age:' pet_age = input('Enter pet age:') # print 'My pet {pet_name} is {pet_age}' print('My pet', pet_name, 'is', pet_age)
Output:
Enter pet name:whiskers Enter pet age:2 My pet whiskers is 2
converting_strings_to_integer.py
# input age with 'Enter pet age:' pet_age = input('Enter pet age:') # All input is a string, so how do you convert it to a number? # hint: use int() number = int(pet_age) # once it is a number, you can do standard math number = number + 5 print("In five years your pet's age will be", number)
Output:
Enter pet age:3 In five years your pet's age will be 8
Links
Exercises (5 mins)
Create a program that uses input and print as follow.
# 1. input name with 'Enter name:' # 2. input age with 'Enter age:' # 3. print 'My age is {age}' # 4. convert age which is a string into variable called {number} # hint: use int() # 5. Add 1 to number # 6. print 'My name is {name} and my age next year is {number}'