Lab 2: Advanced Arithmetic and Definite Loops
Goals
After the lab, you should know how to
- more advanced arithmetic problems
- solve basic looping problems in Python
Objective: Review
Review the slides for today.
Objective: Set Up
Run runHelpClient &
Linux: Set up for Lab 2
- Create a directory called
lab2
in yourcs111
directory. Your programs and the output for this lab will all be saved in thelab2
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files lab2.1.py through lab2.7.py.
Your programs will be graded on correctness, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab2.x.out, where x is the problem number.
You can use Python Visualizer to help you see what is happening in your program. This is the visualizer used in the text book.
- (10 pts) Copy problem 1 from lab 1 (lab1.1.py). Modify your program so that
the program prompts the user for values of
i
andj
. As will become common practice, demonstrate your program several times, with several different inputs to demonstrate that it works. - (13 pts) Create a program that converts a given Fahrenheit
temperature to Celsius. The formula to convert is C=5/9(F-32).
Note that you cannot use this formula exactly in your program.
Output should look similar to:
This program prints the Celsius temperature given a Fahrenheit temperature. Enter a Fahrenheit temperature: 70 70 degrees F is 21.111111111111111 degrees C
Note: When you demonstrate this program, especially think about what are good test cases for this program. What answers do you know?
- (15 pts) Write a program that demonstrates the importance of
operator precedence. Your program will get three
integers from a user and assign them to the variables a, b, and c.
Then, print the result of a Python expression that has no
parentheses, using those three numbers. Then, show a different
result from the same expression with one added pair of
parentheses.
Example output (with fake numbers--I'm not showing an equation):
Enter a: 12 Enter b: 34 Enter c: 5 The result of <print out your equation here> is 51. The result of <print out your equation with parentheses here> is 15.
- (13 pts) Distributing Greatest Hits Albums. A
band is putting out their Greatest Hits album and needs to
know how many cds their album requires. Bands like the Ramones
can fit a lot more tracks on a cd than a band like Led Zeppelin.
Write a program that takes the number of greatest hits and the size of the cds (in terms of the number of tracks) and determines how many cds are needed and how many tracks will have to wait for the next Greatest Hits album.
This program determines the number of CDs in a Greatest Hits album. How many greatest hits/tracks do you have? 24 How many tracks fit on a cd? 10 Your album requires 2 cds 4 tracks will have to wait for the next Greatest Hits album.
Note: you may have some grammar issues in your output. We don't know how to fix those yet.
- (13 pts) Using three variables (i, j, and result), assign them
values to calculate and display
result = i % j
. Use assignment and print statements and afor
loop to show the results of i % j, where i = 6 and j increases from 1 to 8. Example output (without the appropriate values filled in):6 % 1 = ? 6 % 2 = ? ...
- (20) "Mind-blowing Math Trick Can Tell Your Age and Shoe
Size" You may have heard about
this silly
trick. You probably didn't think about implementing it in a
program, but you can!
There are several steps, but they're all relatively simple to implement.
- Ask the user for their shoe size--don't allow them to use half sizes using a trick we learned--and their age.
- multiply the shoe size by five
- add 50 to that number
- take that total and multiply it by 20
- add 1,016 to that total
- subtract the year you were born
The result: the first two numbers of the four-digit number are your shoe size and the last two numbers are your age. Extract those two numbers and present them to the user.
Example Run:
This program will figure out your shoe size and your age. Enter your shoe size (not half sizes): 9 Enter the year you were born: 1996 Working... Multiply shoe size by five 45 Add 50 to that number 95 Take that total and multiply it by 20 1900 Add 1016 to that total 2916 Subtract the year you were born 920 Your shoe size is 9 Your age is 20
- Challenge Problem. (16) The Fibonacci sequence
is 0, 1, 1, 2, 3, 5, 8, 13, ... The pattern is that the nth number is
Fn=Fn-1 + Fn-2 for n greater than
1. The initial values of the sequence are defined as F0=0 and F1=1.
Write a program that computes the first 15 numbers in the Fibonacci
sequence.
If you're having difficulty solving this problem, think about: How many times does this loop need to execute? What needs to be repeated? Try solving this problem by hand, calculating and writing out the results. You won't receive any help until we see that you have something written out. Hint: this is a modification of the accumulator design pattern.
- Copy your
lab2
directory into theturnin
directory. If you have every thing set up correctly, you can do this by running turnin.sh lab2 - Clean up: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
- Go into your
cs111
directory. Create the printable lab assignment, using the command:createPrintableLab <labname>
- View your file using the
evince
command. If there are issues with your printout, go back and fix them. - Print the file using the
lpr
command introduced in the last lab. - Python programs: 100 pts; see above for breakdown
Finishing up: What to turn in for this lab
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 10 a.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!