//
you're reading...
Uncategorized

CS1 Lab Problem Set #2

Released January 23 and Lab Submission Due January 30

CS1021C-001: Lab Program 2

Title: Computing Gas Mileage

Topics covered:
Interactive Programming
Data types: integer, floating point
Arithmetic Operations
Assignment Operator
Repetition Structures
Formatted Output

Purpose: Computing a driver’s gas mileage statistics.

Description: You will write a python program that will calculate a driver’s gas mileage statistics from the input data contained in a log of gasoline fill-up purchases. Begin by displaying a brief description of the program. The computer then prompts the user to input a starting mile number given by the auto’s initial odometer reading. The program should then repeatedly prompt for the data associated with each fill-up purchase. Such data contains three (3) numbers: the first being the number of gallons purchased, the second being the total price paid, and third the current odometer reading.

After each purchase the program should print out the miles-per-gallon achieved since the last fill-up. Purchases should be continued to be input until the user enters in –1 for the number of gallons, indicating the sentinel value and the end of the input.

After processing all the input information, the program should calculate and print the combined miles per gallon obtained for all fill-up.

You should properly format the output using special commands that are discussed in the appendix at the end of this document.

Here is a sample session output with user input indicated in blue:

* This program will compute an automobile’s gas mileage statistics *

Enter initial odometer reading: 5000

Fill-up #1
Enter gallons used (-1 to end): 20
Enter price paid: $30.00
Enter odometer reading: 5400

After Full-up #1: Auto travel tested at
400 miles
20.00 miles per gallon
$1.50 price per gallon
13.33 miles per dollar

Fill-up#2
Enter gallons used (-1 to end): 15
Enter price paid: $20.00
Enter odometer reading: 5730

After Full-up #2: Auto travel tested at
330 miles
22.00 miles per gallon
$1.33 price per gallon
16.50 miles per dollar

Fill-up #3
Enter gallons used (-1 to end): -1

Final Computation: After 2 fill-ups the auto tested at
730 miles
35 gallons
20.86 miles per gallon

Happy Trails! Goodbye.

Appendix: Formatting Output in Python

We can print multiple numbers separating them with commas, in which case they are printed separated with a space:

print 2, 1/3, 1/3., 1./3 # 2 integers and 2 floats
2 0 0.333333333333 0.333333333333

# We can also print any structure in Python

a = [1,2,3,4,5,6,7,8] # printing a list
print a  => [1, 2, 3, 4, 5, 6, 7, 8]
b = {'one':1, 'two':2, 'three':3, 'four':4, 'five':5}
print b # {'four': 4, 'three': 3, 'five': 5, 'two': 2, 'one': 1}

The elements of a list are stored in the same order in which they were entered but those of a dictionary are not and thus, they are printed in an apparently random order.

Formatted printing

We can also be explicit about the format that we want our print statements to have. In the following example, the string %s is replaced by an argument that is typecasted to a string (i.e., converted from any type to the string type) and then printed. Multiple arguments are passed as elements of a tuple:

>>> print ‘John is %s’ % 34
John is 34

>>> print ‘%s is %s’ % (‘Jane’, 34.6)
Jane is 34.6

We can also typecast the elements as either integers (i.e., %d) or floats (i.e., %f). The x in %xd, if any, indicates the minimum number of chars used to write the number; spaces not used by the number can be padded with blanks or zeroes. By default, floats are printed with 6 decimal places. We can change this when we specify the format We can also use white-space control chars to line up columns. The most common such non-alphanumeric chars that need to be printed are tabs (i.e, \t) and newlines (i.e.,\n). Here is an example:

print '%d, %0d, %5d, %05d' % (34, 34, 34, 34)
34, 34,    34, 00034

print '%f, %05.2f, %05.1f, %05.0f, %5.1f' % (34.6, 34.6, 34.6, 34.6, 34.6)
34.600000, 34.60, 034.6, 00035,  34.6

print "\tAge\tname\n\t30\tJohn\n\t34.6\tJane"
        Age     name
        30      John
        34.6    Jane

Advertisement

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: