CS303E Project 1

Instructor: Dr. Bill Young
Due Date: Friday, October 9 at 11:59pm

Copyright © William D. Young. All rights reserved.

Help for the U.S. Traveler to Canada

Last summer, I went to Canada for a vacation. I often found myself wondering when I bought something how much it cost in USD, when I priced gas how much it was in USD per gallon, when I was driving my Canadian rental car how fast I was going in mph, how far was the nearest town in miles, and what was the temperature in fahrenheit.

In this assignment, you'll write a simple utility for a U.S. traveler to Canada to easily obtain this type of information. Answering most of these questions merely requires multipling by a conversion factor. For example, a kilometer is equivalent to 0.621371 miles. But currency conversion requires knowing the current conversion rate from Canadian dollars (CAD$) to U.S. dollars (US$). Your utility will ask the user to supply that value as a first step.

Assignment:

Your program runs a loop allowing the user to enter any of several commands and view the conversion. Each iteration of the loop is menu-driven. The user can select any of the following conversions by choosing a number between 1 and 5:
  1. Distance in kilometers to distance in miles;
  2. Speed in kilometers per hour to miles per hour;
  3. Price in Canadian dollars to U.S. dollars;
  4. Gas price in CAD$/liter to US$/gallon;
  5. Temperature in celsius to temperature in fahrenheit.
Each then asks the user to enter a float quantity as the value to convert. You can assume that the values entered are legitimate floats. In addition to the commands above, the user can enter 6 to show the menu or 7 to exit the loop and leave the utility.

Use the following conversion factors:

You'll need to accept from the user the current conversion rate between Canadian and U.S. currency. Print all output values with two digits following the decimal point.

For this assignment, you must use a main() program and at least three other user-defined functions. These don't have to be complicated. For example, I defined kilometersToMiles(km), CADtoUSD(cad, factor), litersToGallons(liter), and celsiusToFarenheit(temp). You can implement these or choose your own functions to define.

Expected Output:

Below is some sample output for this program. You should match this exactly (for these inputs). Note that this is run from the command line. You can run yours from your IDE, but the TAs should be able to run it from the command line.

> python Project1.py

Welcome U.S. traveler! 
This utility allows you to convert from Canadian units 
of distance, speed, temperature, and currency into U.S. units.

Let's get started. How much does a Canadian dollar cost in USD? 0.72

Type a number to select a conversion:
   Distance (kilometers to miles):      1
   Speed (kph to mph):                  2
   Price (CAD$ to US$):                 3
   Gas prices (CAD$/liter to US$/gal):  4
   Temperature (celsius to fahrenheit): 5
   Print this message:                  6
   Exit the system:                     7

Choose conversion (type 6 for menu): 9

ERROR: Answer should be an integer in [1 .. 7]. Try again.

Choose conversion (type 6 for menu): 1
Enter distance in kilometers: 100.5
Distance in miles: 62.45

Choose conversion (type 6 for menu): 1
Enter distance in kilometers: -45.6
Distance in miles: -28.33

Choose conversion (type 6 for menu): 2
Enter speed in kilometers/hour: 90.8
Speed in miles/hour: 56.42

Choose conversion (type 6 for menu): 2
Enter speed in kilometers/hour: -100
Speed in miles/hour: -62.14

Choose conversion (type 6 for menu): 3
Enter price in Canadian dollars: 100.00
Price in USD: $72.00

Choose conversion (type 6 for menu): 3
Enter price in Canadian dollars: -46.00
Price in USD: $-33.12

Choose conversion (type 6 for menu): 4
Enter gas price (CAD$/liter): 1.77
Gas price per gallon: $4.82

Choose conversion (type 6 for menu): help             # '6', '?', 'help', 'menu',
                                                      # 'info' all work here
Type a number to select a conversion:
   Distance (kilometers to miles):      1
   Speed (kph to mph):                  2
   Price (CAD$ to US$):                 3
   Gas prices (CAD$/liter to US$/gal):  4
   Temperature (celsius to fahrenheit): 5
   Print this message:                  6
   Exit the system:                     7

Choose conversion (type 6 for menu): 4
Enter gas price (CAD$/liter): 2.00
Gas price per gallon: $5.45

Choose conversion (type 6 for menu): 5
Enter temperature celsius: -100
Farenheit temperature:  -148.00 degrees

Choose conversion (type 6 for menu): 5
Enter temperature celsius: 100.00
Farenheit temperature:  212.00 degrees

Choose conversion (type 6 for menu): 5
Enter temperature celsius: 0.0
Farenheit temperature:  32.00 degrees

Choose conversion (type 6 for menu): 6

Type a number to select a conversion:
   Distance (kilometers to miles):      1
   Speed (kph to mph):                  2
   Price (CAD$ to US$):                 3
   Gas prices (CAD$/liter to US$/gal):  4
   Temperature (celsius to fahrenheit): 5
   Print this message:                  6
   Exit the system:                     7

Choose conversion (type 6 for menu): quit             # '7', 'exit', 'quit', 'leave',
                                                      # all work here.
Thanks for using our program. Happy traveling!

> 
Display all output values to two decimal places. See the Programming Tips below for more discussion on this.

Turning in the Assignment:

The program should be in a file named Project1.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment project1 under the assignments sections by uploading your Python file. Make sure that you following good coding style and use comments.

Your file must compile and run before submission. It must also contain a header with the following format:

# File: Project1.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date: 
# Description of Program: 

Programming Tips

Using literal constants: If you are using the same literal data multiple times (or even if you're not), it's a good idea to define each data item as a symbolic constant. This prevents mistyping the data, an error which can be very difficult to find. It also groups all significant constants in one easy to find location, making them easy to maintain and modify, if needed.
MILESINKILOMETERS = 0.621371
LITERSINGALLONS   = 3.78541
GALLONSINLITERS   = 0.264172

DISTANCE  = "1"
SPEED     = "2"
...
EXIT      = "7"

ERRORMESSAGE = "\nERROR: Answer should be an integer in [1 .. 7]. Try again."
Defining symbolic constants for the various possible input commands makes your code more readible. For example:
        # Convert distance in kilometers to distance in miles.
        if command == DISTANCE:
           <handle this case>
is probably much easier to understand than:
        # Convert distance in kilometers to distance in miles.
        if command == "1":
           <handle this case>
It is conventional to name such symbolic constants using all capitals; that makes them stand out and reminds you not to re-define them.

Adding robustness: Have you ever been using an interactive system and forgotten what commands are available? In such cases, I really don't want to have to remember that '6' is the command to show the menu. So when I build such systems, I build in alternatives like '?', 'help', 'menu' and 'info'. To exit the system I want 'exit' and 'quit' to work. So, I typically add those alternatives, even if I don't "advertise" them as part of the standard system interface. Bottom line: if you're ever stuck, try typing 'help'. But that leads to the following:

Choose the correct type: Within the command loop, the user inputs a number (1, ..., 7) but it's read as a string. You could convert it to a int, but there's really no reason to do that because you never do any arithmetic using these inputs. Why not just leave them as strings.

There's another good reason not to convert them. If you want to add alternative commands, like "help", "exit", etc. then you don't need to hassel with dealing with commands that can be either strings or integers. For example,

        # Print thanks message and exit.
        elif command == EXIT or command == "quit" or command == "leave":
            print( THANKSMESSAGE )
            return
I often add such additional commands that aren't part of the "official" interface. IMHO, in almost any complicated system you should be able to type "help" and get some useful guidance.