IT 116: Introduction to Scripting
Homework 7

Due

Sunday, March 24th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

get_int


miles_to_kilometers_table


miles_to_kilometers


kilometers_to_miles_table


kilometers_to_miles

Test Code


Output

Suggestions

  1. Copy only the function headers for the five functions listed above into your script file.
    Under each header, write pass.
    Make sure this statement is indented.
    Run the script.
    Fix any errors you find.
  2. Remove the pass statement from get_int.
    Replace it with an assignment statement that uses input to give the variable number a value.
    Use the parameter prompt as the argument to input.
    Use an assignment statement to convert number into an integer.
    Return number.
    Copy the test code to the bottom of the script file.
    Comment out all but the first two lines of the test code.
    Run the script.
    Fix any errors you find.
  3. Remove the pass statement from miles_to_kilometers_table.
    Replace it with a statement that prints the labels "Miles" and "Kilometers".
    Then write a statement that prints a line of dashes, -.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  4. Write a for loop that gives the loop variable miles values from min to max.
    You will have to use range to do this, and be careful about the 2nd argument.
    Inside the loop print the value of miles.
    Run the script.
    Fix any errors you find.
  5. Add a second argument to the print statement in miles_to_kilometers_table.
    This argument will be a call to the function miles_to_kilometers with miles as the argument.
    Remove the pass statement from miles_to_kilometers.
    Replace it with a statement that returns the parameter miles.
    Run the script.
    You should see two values for miles on each line.
    Fix any errors you find.
  6. Remove the return statement in miles_to_kilometers.
    Replace it with an assignment statement that gives the variable km the converted value of miles.
    You will do this using the formula
    km = miles * 1.609344
    Turn the value of km into an integer with an assignment statement using round.
    Return the value of km.
    Run the script.
    You should see both the miles and km values on the same line.
    Fix any errors you find.
  7. Now you have to make sure the km values align with the "Kilometers" label.
    This can be done using the escape sequence for the tab character, \t.
    Replace the space between "Miles" and "Kilometers" in the label with \t.
    Now we need to print miles and km with \t in between.
    To make this work we will need to concatenate the three values.
    But numbers cannot be concatenated with strings.
    So we have to run the str string conversion function on both miles and km.
    Then we can use concatenation to put \t between them.
    Run the script.
    Fix any errors you find.
  8. Repeat steps 3 - 7 for the last two function making appropriate changes.
    Use the conversion formula
    miles = km / 1.609344

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.