Inputs

Our Python programs would be pretty limited if we couldn’t interact with our users and receive specific data from them. Thankfully, Python has a built-in function called input() for this purpose.

>>> input("Enter your age: ")
Enter your age: 16		# The program prompts the user and "waits" for the ENTER key
'16'	# Because the user input isn't saved in a variable, we can't do anything with it.
>>> age = input("Enter your age: ")
Enter your age: 16
>>> print(age)
16

Penny Problem

Write a Python program that asks the user for a number of pennies. The program should then calculate and print how to convert these pennies into nickels and pennies using the smallest number of coins. For example, 17 pennies can be converted into 3 nickels and 2 pennies.

# pennies.py

# ask user for number of pennies
num_pennies = input("How many pennies do you have? ")
num_pennies = int(num_pennies)	# Need to convert the user's string to an integer

# calculate number of nickels and pennies
nickels = num_pennies // 5
pennies = num_pennies % 5

# print result to the screen
print(num_pennies, "pennies can be converted into", nickels, "nickels and", pennies, "pennies.")

Now that we know how to query the user for data, let’s return to the alarm clock problem from Lesson 2.1 and add some input() statements. Here’s the problem again:

Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off.

Using our partial solution, alarm.py, let’s add the needed statements:

# alarm_clock.py

# define variables
current_time = input("What is the current time? ")	# Note the extra space?
added_hours = input("How many hours before the alarm? ")

# re-typing the input as integers
current_time = int(current_time)
added_hours = int(added_hours)

# calculate alarm time
alarm_time = (current_time + added_hours) % 24

# print result to screen
print("Your alarm will go off in ", added_hours,"hours. Your clock will read", str(alarm_time) + ".")

Exercise 2.9

Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.

# area.py

# ask user for length and width
width = input("Enter the width: ")
length = input("Enter the length: ")

width = int(width)
length = int(length)

# calculate area
area = width * length

# print result to screen
print("The area of the rectangle is", area)

Exercise 2.10

Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Print a nice message with the answer.

# mpg.py

# ask user for miles driven and number of gallons
miles = input("Enter the number of miles driven: ")
gallons = input("Enter the gallons of gas used: ")

miles = int(miles)
gallons = int(gallons)

# calculate mpg
mpg = miles / gallons

# print result to screen
print("Your miles per gallon ratio is", mpg)

Homework

Read HTCS, ch. 2.8-2.11