IT 116: Introduction to Scripting
Homework 6
Due
Sunday, March 9th at 11:59 PM
What You Need to Do
- Create the script hw6.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Move it to an an hw6
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw6.py
Specification
- This script prints a table of a table of inches and their
equivalent centimeter value
- Inches can be converted to centimeters using the formula
cm = inches * 2.54
- Centimeter values must be integers
- The table will run between a minimum and maximum value
entered by the user
- The centimeter values must be converted to integers
- The script must ask the user for maximum and minimum
inches
- It should then print a blank line
- The labels "Inches" and "Centimeters" should appear at
the top of the table
- Under these labels should be a line of dashes
- Then it prints a table of inches and their centimeter
values
- The centimeter values should align with the "Centimeters"
label
- In other words, the output will look the same as
hw5.py
- But it will produce this output in a different way
- The script will create the table using 3 functions which
you must write.
- print_table
- print_header
- print_line
Functions
print_table
print_header
- Header
def print_header():
- This function should print the labels "Inches" and
"Centimeters"
- It should then print a line of dashes
print_line
- Header
def print_line(inches):
- This function will print one line of the
conversion table
- It will convert the inches value into centimeters
- Then it will print the inches and centimeters values
- See the output below
Test Code
Output
Suggestions
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
Copy only the function headers for the three functions listed above into
your script file.
Under each header, write pass
.
pass
is a special Python statement that does nothing.
You use it in situations like this.
Make sure each pass
statement is indented.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
print_table.
Print the parameters min and max.
Copy the test code above to the bottom of your script.
Run the script.
Fix any errors you find.
-
Replace the
print
statement in print_table
with a call to print_header.
Remove the pass
statement from print_header.
Add a print
statement for the labels at the top of the table.
You will need to put a Tab character between the two words so everything
will line up.
You do this with an escape sequence.
Add another print
statement for the line of dashes.
Run the script.
After the input prompts you should see
Inches Centimeters
-------------------
Fix any errors you find.
-
In print_table add a
for
loop
using the loop variable inches and the
range
function.
Range should produce all the values between
min and max.
Think carefully when choosing the arguments for range
.
Inside the loop print the value of inches.
Run the script.
Fix any errors you find.
-
In print_table replace the
print
statement inside the for
loop
with a call to print_line.
Inside print_line remove the pass
statement.
Replace it with a statement that prints the value of the parameter
inches.
Run the script.
The output should be the same as above but now it comes from
print_line.
Fix any errors you find.
-
Remove the
print
statement from print_line.
In it's place you need to write an assignment statement that sets the value of the
local variable cm.
Use the same formula you used in homework 5 to get the value for
cm.
Print the values of both inches and
cm.
Run the script.
Fix any errors you find.
-
Change the
print
statement in print_line
so it prints a single string with a Tab character between
inches and cm.
You will have to use the conversion function str
to change
both variables to a string.
The values should line up with the labels for each column.
Run the script.
Fix any errors you find.
Testing on Your Machine
Copy the Script to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it116 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw6 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.