Python 1: Introduction to Python
Homework 5
Due
Sunday
Deliverables
There is one deliverable for this assignment
Specification
Your script should ask the user for a minimum Celsius temperature and then a maximum
Celsius temperature.
It should then print out a Celsius to Fahrenheit conversion table for this range
of Celsius temperatures.
The table should have the same format as in homework 5.
This script must contain two functions
- print_table_line
- print_conversion_table
print_table_line
This function must have the following header
def print_table_line(celsius):
This function prints one line in the table.
The line consists of the Celsius temperature, a tab, and the
Fahrenheit temperature.
It should print a line that looks like this
0 32
In order to do this, the function must first convert the Celsius
temperature into a Fahrenheit value.
Both temperatures must be integers.
print_conversion_table
This function must have the following header
def print_conversion_table(min, max):
This function does four things
- Prints the words "Celsius" and "Fahrenheit" to label the columns
- Prints a line of dashes, -
- Have a
for
loop
- Call print_table_line(celsius) inside the loop
The for
loop variable must range from the minimum Celsius temperature to the
maximum Celsius temperature.
Each pass through the loop must call print_table_line(celsius)
with a Celsius value.
Suggestions
- Create the file hw6.py and add the header
for the function print_table_line(celsius).
For the body of the function write print
statement that simply prints the
value of the celsius parameter.
Add the following statement after at the bottom of the script
print_table_line(30)
Run the code and fix any errors.
- In print_table_line you need to convert celsius into
into a Fahrenheit value.
Do this my adding an assignment statement about the print statement.
This statement should calculate Fahrenheit value and assign it to the variable
fahrenheit.
Change the print
statement so it print both the celsius
and fahrenheit values.
Run the code and fix any errors.
- Remove the
print
statement at the bottom of the script.
Below the first function, add the header for the print_conversion_table function.
There must be a blank line between the end of the previous function, and the
header of the new one.
Add a print
statement that prints the labels "Celsius" and "Fahrenheit".
Add a print
statement that prints a line of dashes, -.
Add a blank line below this function.
Add the test code below to the bottom of your script.
Run the code and fix any errors.
- Add a
for
loop will a loop variable that will run from the minimum to the maximum
values given by the parameters.
In the for
loop add a print
statement that prints the loop variable.
Run the code and fix any errors.
- Remove the
print
statement under the for
loop.
In its place call the function print_table_line with the loop variable
as the argument.
Run the code and fix any errors.
- Adjust the
print
statement in print_table_line
so the Celsius and Fahrenheit values line up with the labels printed by
print_conversion_table.
Run the code and fix any errors.
Testing
The following test code must be added to the bottom of your script
min = int(input("Please enter the starting temperature for the table: "))
max = int(input("Please enter the ending temperature for the table: "))
print()
print_conversion_table(min, max)
Your output should look something like this
Please enter the starting temperature for the table: 10
Please enter the ending temperature for the table: 15
Celsius Fahrenheit
------------------
10 50
11 52
12 54
13 55
14 57
15 59
The text in blue is what you enter at the command line.