CPSC 115L: Introduction to Computing Spring 2010

Homework 3

CPSC 115-01: due Monday, September 27
CPSC 115-02: due Tuesday, September 28

1. Printing lyrics

Complete Exercises 1 and 2 in Chapter 3 of Downey. Upon completing each exercise, save the result of your run and explain why you get such a result.

2. Fuel-cost calculator

Design and implement a Python script to solve the following problem: calculate the cost of an automobile trip based on (i) the distance traveled in miles, (ii) the cost of a gallon of gas, and (iii) the EPA mileage rating for your car in miles per gallon. For this, design and implement a function named print_cost. This function should have three parameters, one for a distance traveled, one for a cost of a gallon of gas, and one for an EPA mileage rating, and outputs the total trip cost. After implementing the function print_cost, write a code segment to invoke it three times. Your script should behave as follows:
Enter the distance traveled for the 1st trip (in miles): 204
Enter the cost of a gallon of gas for the 1st trip (in dollars): 2.954
Enter the EPA mileage rating for the 1st trip (in MPG): 32
Your total cost is 18.83175 dollars.

Enter the distance traveled for the 2nd trip (in miles): 102.5
Enter the cost of a gallon of gas for the 2nd trip (in dollars): 2.562
Enter the EPA mileage rating for the 2nd trip (in MPG): 25
Your total cost is 10.5042 dollars.

Enter the distance traveled for the 3rd trip (in miles): 121
Enter the cost of a gallon of gas for the 3rd trip (in dollars): 2.789
Enter the EPA mileage rating for the 3rd trip (in MPG): 25
Your total cost is 13.49876 dollars.
You should first begin with your I/O specification and algorithm for this problem. Then implement the function print_cost and write a code segment to invoke it in a Python script named fuel_cost.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named fuel_cost.out. Put a file header at the top of each file.

3. Future-value calculator

Design and implement a Python script to determine the future values of an investment. In particular, given the amount of an initial investment and an annual interest rate, your script should output the future values of the given investment for the first three years. For this, design and implement a function named print_next_value that have two parameters, one for a current amount of the principal and another for an interest rate, and outputs the amount of the principal after one year. After implementing the function print_next_value, write a code segment to invoke it three times. Your script should behave as follows:
Enter the initial value of an investment: 1000
Enter an annual interest rate: 0.025
After one year, your investment is worth 1025.0 dollars.
After one year, your investment is worth 1050.625 dollars.
After one year, your investment is worth 1076.890625 dollars.
As before, you should first begin with your I/O specification and algorithm. Then implement the function print_next_value and write a code segment to invoke it in a Python script named future_values.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named future_values.out. Put a file header at the top of each file.

What to hand in

Submit the following in paper:

Plagiarism and academic dishonesty

Please remember our course policy on plagiarism and academic dishonesty: You are encouraged to consult with one another when you work on homework assignments, but in the end everyone must do one's own work to hand in. In particular, discussion of homework assignments should be limited to brainstorming and verbally going through strategies, but it must not involve one student sharing written solutions with another student. In the end everyone must write up solutions independently. If you have discussed with classmates or used any outside source, you must clearly indicate so on your solutions and provide all references. Turning in another person's work under your name is plagiarism and qualifies as academic dishonesty. Academic dishonesty is a serious intellectual violation, and the consequences can be severe. For more details, read the Student Handbook 2010–2011, pp. 21–29.


* CPSC 115L home page
Valid HTML 4.01!