
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.
Use the following conversion factors:
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.
> 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.
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:
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.