
In this assignment, you'll input a length as some number of feet and inches, and convert that to a number of other length values. Finally, you'll print out the results in a nice format. See the examples below.
How to ask for user input: In your program you'll need to prompt the user to enter the number of feet and the number of inches to convert. The input function is not covered until slideset 3, but it's pretty easy. To get the values of the feet and inches into two variables inputFeet and inputInches, you'll include the following two statements:
inputFeet = float( input("Enter number of feet: ") )
inputInches = float( input("Enter number of inches: ") )
When each statement is executed, it prints the prompt (e.g., "Enter
number of feet: ") on the screen, and waits for the user to enter a
number. After the user types in a number, say 10000.0, this number is
read by the program as a string, say "10000.0". Then the
function float() will convert that string into the
corresponding float value which will be stored in the
variable inputFeet. The second statement will do an analogous
thing for variable inputInches. From there you're good to
go. Notice we didn't name those variables feet
and inches because we'll use those for some other values.
Notice also that in Python ints are treated as floats; so it's OK to
input either an int or float.You can assume that the values entered are positive floats. (It would be easy to convert this program to accept only ints; that would only require changing one word in each input statements above. Can you guess what you'd change?) Your program doesn't have to check that inputs are positive numbers and doesn't have to work if the user enters a bad value. We won't test your program on illegal values.
Doing the conversions: After you've input those two values, you'll then do the conversions indicated in the following table. We suggest you do them in this order.
| inches | inputFeet times 12, plus inputInches |
|---|---|
| feet | inches divided by 12 |
| yards | feet divided by 3 |
| miles | feet divided by 5280 |
| meters | feet times 0.3048 |
| centimeters | meters times 100 |
| millimeters | centimeters times 10 |
| kilometers | meters divided by 1000 |
Print the report: You will then print out a nice report of the conversion following the sample output below. There is a single space after each colon and two spaces before the items that are indented. Leave blank lines where shown. Print each number in the English/Metric Units section of the report with 4 digits following the decimal place.
To generate a string to print representing a float in variable x with 4 digits of precision you can do the following: format( x, "0.4f"). See also the discussion in the Programming Tips section of HW3.
>>> var = 12.3456789
>>> format(var, "0.4f")
'12.3457'
>>> print("The value of var is:", format(var, "0.4f"))
The value of var is: 12.3457
>>>
Note that your program must work for arbitrary legal input values, not merely the values illustrated below. So be sure to test it on a variety of input values, such as 0 and negative values. (But for the values shonw in the samples below, your output should match exactly.)
> python ConvertUnits.py Enter number of feet: 5 # ints are OK here Enter number of inches: 7 5.0 feet and 7.0 inches equals: English Units feet: 5.5833 inches: 67.0000 yards: 1.8611 miles: 0.0011 Metric Units meters: 1.7018 centimeters: 170.1800 millimeters: 1701.8000 kilometers: 0.0017 > python ConvertUnits.py Enter number of feet: 2000.5 Enter number of inches: 0 2000.5 feet and 0.0 inches equals: English Units feet: 2000.5000 inches: 24006.0000 yards: 666.8333 miles: 0.3789 Metric Units meters: 609.7524 centimeters: 60975.2400 millimeters: 609752.4000 kilometers: 0.6098 >
Your file must compile and run before submission. It must also contain a header with the following format:
# File: ConvertUnits.py # Student: # UT EID: # Course Name: CS303E # # Date: # Description of Program:
If you submit multiple times to Canvas, it will rename your file name to something like ConvertUnits-1.py, ConvertUnits-2.py, etc. Don't worry about that; we'll grade the latest version.
Naming Variables: It's typically considered lousy programming practice to use single letter variable names. It's almost always much better to use descriptive names. Obvious variable names for this assignment are inches, feet, miles, etc.
Also, it's always a good idea to comment any part of your code that would be unclear to someone reading it later. The individual steps of this algorithm are individually pretty clear, especially if you use descriptive variable names, so it's not really necessary to comment them. But there needs to be an overall comment that says what the program accomplishes. The question to ask yourself is: If someone besides me looks at this code, how hard will they have to work to understand what's going on?
Output format: As explained in slide set 1, there are multiple ways to run your program. If you run in interactive mode (in the Python loop), the system will automatically display the result of every command (unless the result is None). If that result is a string, it will display with string quotes ('answer'). If you're running the program in batch mode (from the command line, as e.g. python myProgram.py) or explicitly print a string, it won't appear with string quotes. If you run your program in batch mode the only things you'll see displayed are the things you explicitly print from your program; it won't display the results of individual commands. The following is in interactive mode:
>>> string = "my string" >>> string 'my string' >>> print(string) my string >>>If your string output doesn't match what's shown in the assignment sample output because of the presence or absence of string quotes, that may be the reason. Usually, it's not an issue to worry about.
But remember, you must turn in a file that contains the code to do this computation. So it's not adequate to just run the steps in interactive mode. You can do that while you're debugging the steps; but you need a complete program stored in a file for your submission.