To Lecture Notes

IT 212 -- Sep 23, 2020

Review Exercises

  1. Zoom Poll 4: Review Exercises:
    1. The bool method converts a value to boolean (True or False). The rule is that zero converts to False, but everything else converts to True. The null string "" is considered to be "zero" or False for the bool method.  Every other string, including "False" is considered to be non-zero. What is the value of this expression?
      bool(0)
      a. True    *b. False
      
    2. What is the value of this expression?
      bool(961)
      *a. True    b. False
      
    3. What is the value of this expression?
      bool(961)
      *a. True    b. False
      
    4. What is the value of this expression?
      bool(961)
      *a. True    b. False
      
    5. What is the value of this expression?
      bool(961)
      *a. True    b. False
      
    6. What is the value of this expression?
      bool("orange")
      *a. True    b. False
      
    7. What is the value of this expression?
      bool("False")
      *a. True    b. False
      
    8. What is the value of this expression?
      bool("")
      a. True    *b. False
      
    9. What is the value of this expression?
      bool(0.0000001)
      *a. True    b. False
      
    10. What is the value of this expression?
      bool(0.0000001)
      *a. True    b. False
      
    11. What is the value of this expression?
      bool(0.0)
      a. True    *b. False
      
    12. What is the value of this expression?
      int(bool(3482))
      a. 0    *b. 1    c. 3482
      
  2. Identify the arguments and the parameters:
    # --------------------------
    # Module name: my_method1.py
    def square(n):
        return n ** 2
        
    # -------------------------
    # Module name: main.py
    import my_method1
    print(my_method1.square(7))
    # ------------------------
    
    Ans: Arguments are values passed in to a method when it is called. Parameters are variables passed in to a method when it is defined with a def statement. The parameters tell the method what to do with the arguments.

    n is the parameter for the square method, 7 is the argument for the square method, and my_method1.square(7) is the argument for the print method.
  3. Identify the arguments, parameters, and local variables:
    # ---------------------
    # Module: my_method2.py
    def sum_array(the_array):
        total = 0
        for n in the_array:
            total += n
        return total
    
    # --------------------
    # Module: main.py
    from my_method2 import sum_array
    a = [1, 2, 3, 4]
    print(sum_array(a))
    # --------------------
    
    Ans: the_array is the parameter for the sum_array method. a is the argument for the sum_array method in main; sum_array(a) is the argument of the print method. total and n are local variables in main.
  4. Look at Problem 5 from extract-methods.zip
    Ans:extract-methods-ans.zip.
  5. Give an example of a list of lists. A list of lists is also called a ragged array.
  6. Look at Problem 6 from extract-methods.zip
    Ans:extract-methods-ans.zip.
  7. The ragged array defined by game3.txt records the number of bowling pins knocked down by each ball. Compute the score for the game. The score for the game is the number of pins knocked down plus bonuses for spares and strikes. The bonus for a spare is the number of pins knocked down by the next ball; the bonus for a strike is the number of pins knocked down by the next two balls.
    Ans: Here are the calculations for the input file game3.txt.

Project 2

Introduction to Classes