CSci 157 - Lab 9

The purpose of this lab is to gain experience with looping constructs.

Part I.  Testing Loops
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

Run each function and record the results of each run. For the two that request user input, try them with input of your own devising but record what values you input along with the results. If the result is an infinite loop - record that. Remember that typing <Ctrl-C> will interrupt an out-of-control program.

Write up the results (cleanly, legibly, and well labeled) for turn in, either on paper on in a text editor.

Now, make the following changes to the functions sumVals and debits:

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.

  1. 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.