1
h08
CCS CS20 S18
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 Thu 05/24 12:30PM Thu 05/31 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

This assignment should be submitted by scanning the pages in the correct order to a PDF file and uploading to gradescope.com.

For more information, visit ucsb-cs8.github.io and look for Gradescope: Student Self Submission under "topics".

Even though it is a Gradescope submission, nevertheless, *please fill in the information at the top of this homework sheet*, including your name and umail address.


READING ASSIGNMENT

Please read Perkovic 5.1-5.2 (decision control,accumulators,nested loops). Then complete these problems.

  1. (10 pts) These 10 points are for filling in your name at the top of the sheet, and scanning correctly on Gradescope. (Having these here gives me a place to give you feedback if there is some glitch with the way you are doing it.)

  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?