Skip navigation
Homework 2: Careful Calculations
- Assigned
- Monday, September 5, 2016
- Due
- 11 p.m., Thursday, September 8, 2016
- Summary
- In
this assignment, you will write your first Python programs. You will
practice using variables and arithmetic operations.
- Collaboration
- You will work with an assigned partner.
- Submitting
- Submit two Python programs using the online turnin form. See below.
- Scoring
- 10 points
Precondition: Choose a Text Editor
For writing code, you will need to decide on an editor to use. Most
programs are simply structured text, so we can use a basic text editor
to create the programs. Many text editors available to you also have
some built-in features that will make your life easier while coding:
Syntax highlighting, automatic indentation, and so on.
There are many different editors available to you, so feel free to
try them all. Here is a sample of available editors:
- brackets
- A popular editor that is installed on all lab computers.
If you don't know anything about text editors, and don't
want to think about this decision, this is a good place to start.
- emacs
- This is a very mature and capable editor. It has many
bells and whistles to
help with coding. Some of the commands to use it are a little odd, but
there are numerous
tutorials available.
- vi
- This is a very user-unfriendly editor that is extremely
cryptic and has a very steep
learning curve, but once you've learned it, it is very powerful. (If
you're curious about the tradeoff between usability and power, take my
HCI class.)
Precondition: Obtain the homework template
Download the homework template, template.py. This template
contains some lines
at the top that you and your partner should fill in. Failure to
use the template
may result in loss of points on the assignment.
For each of the files that you are to create, just save a copy of the
template under that name.
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, mileage.py
and time.py
, using the turnin form.
Again, if you have any problems here, don't hesitate to ask your
professor, or the lab aides or class mentors.
Janet
Davis (davisj@whitman.edu)
This assignment was adapted from one developed by Sarah Diesburg and
shared through EngageCSEdu. Thanks to Tracy Peterson and
Wally Herbranson for humoring us. Thanks to Andy Exley for developing
the preconditions.
Created September 2, 2016
Last revisedSeptember 09, 2016, 01:53:18 PM PDT
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.