IT 116: Introduction to Scripting
Homework 5
Due
Sunday, February 25th at 11:59 PM
What You Need to Do
- Create the script hw5.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw5
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 hw5.py
Specification
- This script prints a table of kilometer values
and their equivalent Miles values
- It must do this using a
for
and the range
function
- See
Class Notes 10
for details
- kilometers can be converted to Miles using the
formula
miles = km / 1.609344
- The Miles values must be converted to integers
- This must ask the user for maximum and minimum
kilometer values
- It should then print a blank line
- The labels "Kilometers and "Miles" should appear at the
top of the table
- Then it prints a table of kilometers and their
equivalent miles values
- Under these labels should be a line of dashes
- The centimeter values should line up with the
"Miles" label
- See the example below
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
-
Get values for the variables min and
max using assignment statements and the
input
function.
Convert both min and
max to integers.
Print max and min.
Run the script.
Fix any errors you find.
-
Remove the
print
statement you
created above.
Write a print
statement that prints a blank line.
Print the labels "Kilometers" and "Miles".
Print a line of dashes under the labels.
Run the script.
Fix any errors you find.
-
Write a
for
using the loop variable
km 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 km.
Run the script.
Fix any errors you find.
-
Remove the print statement inside the
for
loop.
Use the formula above to calculate the miles value and store
it in the variable miles.
Now you need to use round
to turn this value into an
integer.
You do this with an assignment statement, setting
miles to the rounded value you calculate
above.
Print both km and
miles.
Run the script.
Fix any errors you find.
-
Now you have to make sure the miles values
align with the "Miles" label.
This can be done using the escape sequence for the tab character,
\t.
Replace the space between "kilometer" and "Miles" in the label
with \t.
Now we need to concatenate the values of km
and miles into a string with
\t between them.
But numbers cannot be concatenated with strings.
So you have to run the str
string conversion function
on both km and
miles.
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 hw5 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 © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.