Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. De- velop a Python program that prompts the user to input the miles driven and gallons used for each tank- ful. The program should calculate and display the miles per gallon obtained for each tankful. After

processing all input information, the program should calculate and print the combined miles per gal- lon obtained for all tankful (= total miles driven divided by total gallons used).

What will be an ideal response?


```
# Calculates miles per gallon.

# initialization phase
gallons = 0 # number of gallons per tank
totalGallons = 0 # total number of gallons used
miles = 0 # miles drive per tank
totalMiles = 0 # total miles driven

while -1:

# assign gallons used for one tankful to gallons
gallons = raw_input( "Enter the gallons used (-1 to end): " )
gallons = float( gallons )

# terminate on sentinel value
if gallons == -1:
break

# assign miles driven for one tankful to miles
miles = raw_input( "Enter the miles driven: " )
miles = float( miles )

# increment total number of miles, gallons and tanks
totalMiles += miles
totalGallons += gallons

# prints the mileage for this tank
print "The miles / gallon for this tank was", ( miles / gallons )

# prints the total mileage
if totalGallons != 0: # if no user input, avoid division by zero
print "The overall average miles / gallon was", \
( totalMiles / totalGallons )
else:
print "Attempted to divide by zero gallons."
```
Enter the gallons used (-1 to end): 12.8
Enter the miles driven: 287
The miles / gallon for this tank was 22.421875
Enter the gallons used (-1 to end): 10.3
Enter the miles driven: 200
The miles / gallon for this tank was 19.417474757282
Enter the gallons used (-1 to end): 5
Enter the miles driven: 120
The miles / gallon for this tank was 24.0
Enter the gallons used (-1 to end): -1
The overall average miles/gallon was 21.6014234875

Computer Science & Information Technology

You might also like to view...

Answer the following statements true (T) or false (F)

1. In a while loop, the loop-control variable must be initialized before the loop 2. Indentation of loops does not improve the readability of programs 3. The last statement in a sentinel-controlled loop is normally a read statement. 4.The do-while loop is often the loop of choice for a menu-driven program 5. The sentinel value is always the last value added to a sum being accumulated in a sentinel-controlled loop

Computer Science & Information Technology

What are highly-directional antennas typically used for?

What will be an ideal response?

Computer Science & Information Technology

The ____ page replacement policy is based on the theory that the best page to remove is the one that has been in memory the longest.

A. TRU B. LRU C. LIFO D. FIFO

Computer Science & Information Technology

The most common classes of interrupts are: program, timer, I/O and ________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology