| 1 |
| h08 |
| CS8 M19-B |
| Name: | ||||
|---|---|---|---|---|
| (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" | ||||
h08: Perkovic 5.1 (Decision Control and the if Statememt), 5.2 (for Loop and Iteration Patterns)
| ready? | assigned | due | points |
|---|---|---|---|
| true | Tue 08/20 02:00PM | Tue 08/27 02:00PM |
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.)
READING ASSIGNMENT
Please read Perkovic 5.1 (Decision Control and the if Statememt), 5.2 (for Loop and Iteration Patterns). Then complete these problems and turn in your completed homework in lecture on the due date.
- (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
-
(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 ofprint('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
if/elif/else, some of the relationships are implicit. You cannot get to theelifunless the condition on the firstifis false. So you should not check for that a second time. (To be more clear: theelifon p. 129 says:elif t > 32:rather thanif t <= 86 and t > 32. Thet<=86part is unnecessary, because we would never even get to theelifunlesst<=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
(10 pts)
colors = ["red","green","blue"] for c in colors: print(c)(10 pts)
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.
The code
mySum = mySum + numtakes the old value ofmySum, addsnumto it, and stores the result back inmySum.That code is done inside a
forloop,for num in myList, after settingmySuminitially to zero.The final value for
mySumis20. What does that number20represent in this case? -
(10 pts) On p. 135, we see the intermediate values for mySum, namely
3,5,12,11,and10. 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](instead ofnumList=[3, 2, 7, -1, 9].) What would the successive intermediate values ofmySumbe 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 ismyProdinitialized to, and why?