You are on page 1of 1

If you work in a group, write down all group members’ names and SPIRE IDs below.

Also, make
sure you select all members’ names when submitting this questionnaire to Gradescope.
Names & IDs:

Lab 5: while loops

1. In the first program, print_stars.py, if we want to change it so that the first line prints out
n stars, the second line (n-1) stars, third line (n-2) stars, and so on, and the last (n-th) line
1 star, how would you change your program to implement this? An example output is given
below. You do NOT need to write down the actual code, just briefly describe what changes
you would make to the program to achieve the desired output. [4 pts]

Enter a number: 5 Answer:


*****
****
***
**
*

2. Let’s think about the is_prime function. Instead of looping from 2 to 𝑛, inclusive on both
ends, let’s say the programmer made a typo and made 𝑛 exclusive (i.e. instead of <= 𝑛 they
wrote < 𝑛, effectively making the loop terminate at 𝑛 − 1). This seemingly small mistake
can cause incorrect results. Let’s analyze the behavior of this modified function. First, fill in
the missing blanks below. [4 pts]

n Is n a prime? What sequence of numbers the What’s the return value of the
(ground truth) modified loop will iterate through? modified function (True or False)?
4 False None True
9 False 2 True
16 False 2, 3 False
25 False
36 False
49 False
64 False
81 False

Next, based on the above observations, in what cases do you think the modified function will
report incorrect results? [2 pts]

You might also like