In this assignment, you will write your first Python programs on your own. You will practice using variables and arithmetic operations. You will work with a partner, as assigned in class on Tuesday.
Precondition: The homework template
At the top of every Python file you create in homework for this course, include the following text:
Fill in the correct information before you get started.
Approach: Handling customer requests
In this assignment I would like you to :
- Look at the two customer requests below.
- Analyze each request and design an algorithm to solve to the problem. You will later reflect on your approach to analysis and design.
- After you have studied the problems and designed your algorithms, you should write a program to solve each of the problems. Please make sure that you write the code in two separate files saved with the names given below.
- Once you think you have the code working, TEST it thoroughly. Don't simply run it once with one set of values and assume it is good. Test it with three to five different values (or sets of values, actually) and make sure that the numbers the computer gives you agree with the numbers you know to be true or have calculated "by hand". If you find examples that prove your code incorrect, it's your responsibility to fix your solution or report honestly on any problems you discovered but could not fix. You will later reflect on your approach to debugging.
Note that these steps are iterative. As long as your program has bugs, you may need to rewrite your code, reconsider your design, reconsider the problem, or ask more questions about what the customer wants.
Request 1: Miles Per Gallon (mileage.py)
You are contacted by Tracy Peterson who is the administrative assistant for Whitman College's Physical Plant. She is responsible for keeping track of gas mileage for the vehicles in the college motor pool. She wants a tool to help her calculate miles per gallon (MPG).
To compute MPG, you record the starting and ending mileage of the car, subtract to find the total mileage driven, and then divide this by the amount of gas consumed. For example, Sarah Duisburg wrote: "On a past trip to visit my family, my car's odometer was at 92567 when I left and 92783 when I returned. When I refilled the gas tank, it required 10.6 gallons of gas. This comes out to 20.377 miles per gallon (my last car got really bad mileage)."
Ms. Peterson asks you to write a program,
mileage.py, that prompts her for a car's starting and ending mileage, as well as the gallons of gasoline consumed. It then calculates the MPG and prints a message reporting the MPG of the car.
NOTE: Your program should ask for the starting mileage first, the ending mileage second, and the total gasoline consumed last. Failure to following this ordering will result in some point deduction.
Request 2: Time Conversion (time.py)
You are contacted by Dr. Wally Herbranson who teaches psychology at Whitman College. In his research on comparative cognition, he and his students use a computer to time tasks that pigeons learn how to do. Computers measure time in milliseconds, but some tasks, such as the box and banana problem, take longer than a minute to complete.
Dr. Herbranson wants a tool that converts time in milliseconds into time in minutes, seconds, and milliseconds. Remember that:
- There are 1000 milliseconds in a second.
- There are 60 seconds in a minute.
He wants you to write a program,
time.py, that prompts the user for the number of milliseconds. It then breaks the number of milliseconds down into minutes, seconds, and milliseconds and prints out these numbers. For example, an input of 62,523 milliseconds should give an output of 1 minute, 2 seconds, and 523 milliseconds, or 1:02.523.
NOTE: Your program should provide output values in the order specified above. Failure to follow this ordering will result in some point deduction.
Hint: Work backwards. Integer division and the modulus operator will help a lot.
Submitting your work
Submit two files,
time.py, here on Canvas. Make sure you include the template at the top and you include the names of both partners. Please submit a single solution that has both your names on it.
Again, if you have any problems here, don't hesitate to ask your professor, or the lab aides or class mentors.
Janet Davis (firstname.lastname@example.org) This assignment was adapted from one developed by Sarah Diesburg and shared through EngageCSEdu. Thanks to Tracy Peterson and Wally Herbranson for humoring us.