CS51A - Spring 2019 - Class 11

Example code in this lecture


Lecture notes

  • Administrative
       - Assignment 4: pairs
       - Midterm

  • What will be printed out when we run scope.py code?
       - When we run the file, the first two functions will get defined
       - Then, it will execute the three statements at the end of the file
       - The first print statement will print out 20
       - The second print statement will print out 30
          - Why not 60? I.e., in double_input, we assigned 20 to val as the first line.
             - val = 2*val updates the value of the parameter *NOT* the variable outside the function

  • The "scope" of a variable is the portion of the code we can reference that variable in and have it be valid
       - When we declare a variable in the interpreter, what is its scope?
          >>> x = 10

          - the scope is any shell statements/expressions typed after it
       - When we declare a variable (outside a function) in a file, what is its scope?
          - anywhere below it in the file
             - remember, running a program is very similar to typing those commands into the shell
       - When we declare a variable inside a function, what is its scope?
          - anywhere below it in the FUNCTION
       - What is the scope of the parameters?
          - anywhere in the FUNCTION
          - it doesn't really make sense outside of the function since we wouldn't have a value for it

       - Additionally, the scope also defines the context for a variable reference.

       - What would be printed out if we ran scope2.py code?
          - the program starts at the top and declares three "global" variables x, y and z
          - then it declares two functions
          - then it calls mystery1 with 10 and 20
             - the parameter a gets the value 10
             - the parameter z gets the value 20
                - notice that this is *different* than the global variable z!
                - in particular, when we execute z = 100, this reassigns the value of the local z, but not the global
             - The program prints out s, x, y and z
                - s is a local variable
                - x and y are global
                - z is the parameter
             - and then sets the value of the global variable z to 100
          - After mystery1 returns we print out the values of the global variables x, y and z
             - all three are unchanged!
                 - x and y weren't changed anywhere
                - z was changed in mystery1, but the z being changed was the parameter, not the global z

       - What would happen if we added a call to mystery2 at the end and ran scope2.py code?
          - Error. The scope of a is only defined within mystery1, so it cannot be accessed anywhere outside the function.
       - Why do programming languages limit a variable's scope?

  • debugging
       - The quatruple_input function in debugging.py code attempts to quadruple the inputs value by adding it four times
       - However, if you run it, say with 5:
          >>> quadruple_input(5)
          we get the wrong answer

          (In fact, no matter what you run it with, you always get the same answer!)

          >>> quadruple_input(2)
          >>> quadruple_input(10)

       - A "bug" is a behavior in the code that is not intended. Debugging is the practice of trying to find and fix bugs.
       - You might be able to look at the code and find the bug in this example. However, if you can't, you can try and add more information to your program to figure out what the problem is.
       - Adding print statement is one good way to figure out what your function is doing
       - Look at debugging_with_prints.py code
          - If we run this version, we start to see what the problem is:
          >>> quadruple_input(5)
          before loop item is: 5
          item is: 0
          total is: 0
          item is: 1
          total is: 0
          item is: 2
          total is: 1
          item is: 3
          total is: 3
          after loop item is: 3

       - The problem is that we've used the input parameter as the variable in the for loop and the value is getting lost!
       - The fix is to use a different variable name here (e.g., 'i')
       - When you're all done debugging and your code works, make sure to remove the print statements!

  • string methods
       - a few that might be useful (though there are many there)
          - lower
          >>> s = "Banana"
          >>> s.lower()
          >>> s

              - Remember, strings are immutable!

          - replace
          >>> s.replace("a", "o")

          >>> s

             - remember that strings are immutable
             - if you want to update a variable after the method has been applied, you can do the following:

             >>> s
             >>> s = s.replace("a", "o")
             >>> s

          - find
          >>> s.find("a")
          >>> s.find("q")
          >>> s.find("a", 2)

          - count

          >>> s.count("a")
          >>> s.count("b")

  • write a function called find