CSci 157 - Loop Worksheet
For each code example below, do the following:
- Circle and label each of the
initialization, test,
and update (label
with I, T,
and U). If any of these
parts are missing, say so.
- Determine what value(s) of
the loop variable(s) will make the loop terminate.
- Determine how many times the
loop body will be
executed. If one of I, T,
or U are missing, write
down indeterminate.
If the loop will never
terminate, write down infinite
loop.
- What is the output or result
of executing each code fragment? Be as specific as you can;
sometimes nothing is the
appropriate answer.
When you cannot say exactly what the output will be, describe the output you expect.
Number 1: correctly accumulate and output a sum?
Number 2: properly print the first 20 multiples of 5?
Number 3: does it print 'Hello' 4 times?
Number 4: assuming graphics.py has been imported, what does this do?
Number 5: correctly accumulate a sum of numbers entered by the user? (like, tallying the total cost of items scanned at check-out)
Number 6: does it print some number of 'Lower!' strings?
Number 7: correctly accumulate a sum from values in a range?
Number 8: consider calling the function
hailstone(5)
,
what is output?## hailstone : int -> int def hailstone(seed): "Generates the hailstone sequence; seed must be >= 1" print(f'Starting value: {seed}') onetime = True while onetime: if seed % 2 == 1: seed = seed * 3 + 1 else: seed = seedint // 2 print(f'Next value = {seed}') onetime = seed != 1 return seed