TLP - Programming Maintenance
Week 7 - Reading Code - Halloween Candy Edition

This assignment is based on data separately collected by Stacia Kelly and Carston Calderwood during the Choose Your Own Data assignment in the FOP class.

 

Background

One of the skills we need to gain as a programmer (and as a programming teacher) is reading someone else's code to figure out what it does. In this activity you will read several functions and try to figure out what they do and how they do them.

You will need the following files to complete this activity

 

 

 

Task

Before you begin, take a moment to explore the three data files you downloaded so you have a sense for what they represent.

Answer the following questions about the code in candy.py

  1. What does functionOne() do?
    • Begin by ONLY looking at the code and trying to figure out what it will do without executing the code in Thonny.
    • When you think you know, test your theory by invoking the command by running functionOne("Snickers") at the command line.


  2. What does functionTwo() do?
    • Begin by ONLY looking at the code and trying to figure out what it will do without executing the code in Thonny.
    • When you think you know, test your theory by invoking the command by running functionTwo("snickers") at the command line.
      • Please note that since the "spelling" of Snickers/snickers changed between functionOne and functionTwo because they use different data files with different formats.


  3. What does functionThree() do?
    • Begin by ONLY looking at the code and trying to figure out what it will do without executing the code in Thonny.
    • When you think you know, test your theory by invoking the command by running functionThree() at the command line.


  4. Sometimes, your task with a new program is to look at code you "recognize" and to figure out what it actually does. For example, all of the code above used commands you were familiar with. You just needed to figure out how it worked in this context.

    But other times, your task with a new program will be to take a program where you know what it does, and figure out some new feature you have never seen before. That is the case in this last function. This last function does (almost) exactly the same ting as functionThree() but it uses a new data structure you have never seen before.

  5. Consider line 48.
    1. What is new in this line of code?
    2. Go ahead and Google "what does {} do in python" and see what you can learn about this line of code.
  6. Consider line 53.
    1. What type of data is stored in data[2]?
    2. What type of data is stored in data[1]?
    3. Given your answer to 5.2 (the previous question) why is such an unusual command given what we have seen before?
  7. Consider line 63.
    1. What type of data is stored in vals[0]?
    2. What type of data is stored in total?
    3. Given how you answered 6.2, what type of data does have to produce in order for line 63 not to crash or cause an error
  8. Given what you observed and read in 48 and 53, and 63 what do you think the populations variable is doing?

 

You should submit your answers to these questions on Blackboard.