1
h08
CS8 F17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
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-5.2 (decision control,accumulators,nested loops)

ready? assigned due points
true Tue 10/31 08:00AM Tue 11/07 08: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.
There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)


READING ASSIGNMENT

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

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

    Also: while we strongly prefer that you submit your homework on a single sheet of paper, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED.

    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.

  2. (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 if/elif/else, some of the relationships are implicit. You cannot get to the elif unless the condition on the first if is false. So you should not check for that a second time. (To be more clear: the elif on p. 129 says: elif t > 32: rather than if t <= 86 and t > 32. The t<=86 part is unnecessary, because we would never even get to the elif unless t<=86 were 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”.)

  3. 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=",")
    
  4. (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 + num takes the old value of mySum, adds num to it, and stores the result back in mySum.

    That code is done inside a for loop, for num in myList, after setting mySum initially to zero.

    The final value for mySum is 20. What does that number 20 represent in this case?

  5. (10 pts) On p. 135, we see the intermediate values for mySum, namely 3, 5, 12, 11, and 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]
    (instead of numList=[3, 2, 7, -1, 9].) What would the successive intermediate values of mySum be in that case? List them in the space below.

  6. (10 pts) On page 135-136, the textbook discusses accumulating a product instead of a sum. The accumulator variable is called myProd this time. In the version of the code that works properly, what is myProd initialized to, and why?