CSci 157 - Lab 9
The purpose of this lab is to gain experience with looping constructs.
- In a file loops.py, define one function per example of iteration given below, so four functions total.
- Define a
main
function that calls each of your new functions, one after the other. - Call
main
Number 1: call this function sumVals
print("This function sums three values")
sum = 0
for i in range(3):
x = int(input("Next value to sum, please: "))
sum = sum + x
print("Sum = ", sum)
Number 2: call this function times_table
for i in range(1,5):
for j in range(0, 11, 2):
print(i, '*', j, '=', i*j)
Number 3: call this function debits
bankBalance = 500.0
print("Your balance is $", bankBalance)
while bankBalance > 0:
# user enters positive values representing checkcard purchases
checkAmt = float(input("Debit Amt? "))
print(" - $",checkAmt)
bankBalance = bankBalance - checkAmt
print("You are overdrawn by $", -bankBalance)
Number 4: call this function getting_smaller
xm = 10.0
while xm < 100.0:
print(xm, "is less than 100")
xm = xm * 0.5
- In main, call
sumVals
with a number of your choice - For
debits
, changebankBalance
from a regular variable to a parameter, so we can give it different balances - In main, call
debits
with a new balance of your choice - Add a docstring and function signature to each function (note, if a
function takes no arguments and returns no value, its signature is
None -> None
) - Remove the call to getting_smaller
Demonstrate!
Part II. Drawing using Iteration
Switch driver and observer. Make a new program file for the following problem. Use at least one loop in your program;
decide among yourselves if a for loop or a while loop
would be most appropriate. All your program code can go in the main
()
function.
- AlmostBullseye
Write a program called AlmostBullseye.py that draws a "lopsided" bullseye in a window. The biggest circle should be 100 pixels in diameter, while the smallest circle should be 20 pixels in diameter. Each circle in between should be 20 pixels smaller in diameter than the circle surrounding it. The circles will all have the same upper left-hand corners at location (10, 10). Use a loop - each loop iteration draws the next smaller circle.