|(as it would appear on official course roster)|
|Umail address:||@umail.ucsb.edu|| section
| Optional: name you wish to be called|
if different from name above.
| Optional: name of "homework buddy"|
(leaving this blank signifies "I worked alone"
h06: Perkovic 5.1-5.2 (decision control,accumulators,nested loops)
|true||Thu 01/31 02:00PM||Wed 02/06 09:00AM|
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the lowest scores (if you have zeros, those are the lowest scores.)
Please read Perkovic 5.1-5.2 (decision control,accumulators,nested loops). Then complete these problems and turn in your completed homework during your registered lab section..
(10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work.
DO NOT staple, paper clip, spit-fold-and-tear, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.
(40 pts) p. 129 shows a function definition for a multi-way if/else that prints a message depending on the temperature.
Rewrite this function so that instead of printing a message, it returns a letter grade (e.g.
return 'A'instead of
print('It is hot')based on the integer parameter. If the grade is 90 or above, return an ‘A’. If it is 60 or higher, but less than 90, return a ‘C’, and if it is less than 60, return an ‘F’. (In real life, there would be Bs and Ds, but this is just an exercise.)
NOTE: Be careful about the fact that in an
else, some of the relationships are implicit. You cannot get to the
elifunless the condition on the first
ifis false. So you should not check for that a second time. (To be more clear: the
elifon p. 129 says:
elif t > 32:rather than
if t <= 86 and t > 32. The
t<=86part is unnecessary, because we would never even get to the
t<=86were true. Make sure you keep this in mind as you write your code for this problem. Points may be deducted if you do redundant checks, even if the code “works”.)
For the Python code in the left box, write the output in the right box
colors = ["red","green","blue"] for c in colors: print(c)
fruits = ["apple","banana","pear","grape"] for i in range(4): print(i,fruits[i],sep=",")
(10 pts) p. 134-136 discusses the “Accumulator Pattern”, which is a very important topic in this course; one of the most important for you to master. So please read those two pages several times and try to understand every detail. The figure at the top of p. 135 shows the various stages of execution for the code on p. 134.
mySum = mySum + numtakes the old value of
numto it, and stores the result back in
That code is done inside a
for num in myList, after setting
mySuminitially to zero.
The final value for
20. What does that number
20represent in this case?
(10 pts) On p. 135, we see the intermediate values for mySum, namely
10. Try to understand where those values come from as the loop progresses.
Then, imagine the same loop were executed, but with the first line of code being
numList=[9, 3, 1, 1, 7]
numList=[3, 2, 7, -1, 9].) What would the successive intermediate values of
mySumbe in that case? List them in the space below.
(10 pts) On page 135-136, the textbook discusses accumulating a product instead of a sum. The accumulator variable is called
myProdthis time. In the version of the code that works properly, what is
myProdinitialized to, and why?