IT 116: Introduction to Scripting
Homework 4
Due
Sunday, September 28th at 11:59 PM
What You Need to Do
- Create the script hw4.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Move it to an an hw4
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 hw4.py
Specification
- Light is electromagnetic radiation
- It has a frequency and a wavelength
- The different colors we see are light of
a certain wavelength
- We can only see light of a specific range
of wavelengths
- Write a script that will take the wavelength
of light in nanometers and turns it into a color
- The color for different wavelengths is given by
the following table
Color | Wavelength (nm) |
No color | Greater than 760 |
Red | 610–760 |
Orange | 590–609 |
Yellow | 570–589 |
Green | 500–569 |
Blue | 450–499 |
Violet | 380–449 |
No color | Less than 380 |
Output
- The output should look something like this
Wavelength: 500
Green
- The text in blue is what the
user enters
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
-
Write an assignment statement which asks the user for a
wavelength and stores it in the variable
wavelen.
Convert the value of wavelen using
int()
.
Print the value of wavelen .
Save the file and quit.
Run the script to make sure it works properly.
If it does not work, fix the problem.
-
Remove the print statement you wrote in the previous step.
Replace it with an if statement that will run if the value of
wavelen is more than 750.
Inside the if
write an assignment statement that
gives color the value "No color".
Outside this
statement, print the value of
color.
Run the script.
Fix any errors you find.
-
Underneath the
if
statement but before
the print
statement, write and elif
clause that runs if the value of wavelen
is greater than or equal to 610.
Inside the elif
clause write an assignment statement
that gives color the value "Red".
Run the script.
Fix any errors you find.
-
Repeat the steps above for the remaining colors.
-
After the last
elif
clause, write an assignment statement
that gives color the value "No color".
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 hw4 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
Testing the Script on Unix (Optional)
- Connect to pe15.cs.umb.edu
using an ssh client like putty.exe (Windows)
or ssh (Mac)
- Go to the directory for this exercise
cd it116/hw/hw4
- Run this script
python3 hw4.py
- You should see something like this
Wavelength:
Green
- The text in blue is what you
enter at the command line
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.