CS5nm, UC Santa Barbara, Fall 2008         E02 practice, 11/12/2008

Name __________________________________________________________________

UMail Address: ____________________________________________ @ umail.ucsb.edu

This in-class exercise reviews scope of variables (local vs. global), and fruitful vs. void functions
(Textbook: Chapters 3 and 6, especially Section 3.9)

On this exercise, you may collaborate with others in answering these questions.

  1. Suppose we have the following function definitions:

    def printTwice(msg):
        print msg
        print msg
    
       
    def catAndPrintTwice(part1, part2):
        cat = part1 + part2
        printTwice(cat)
    
    1. (1 pts) Is the function printTwice() fruitful or void?

      Circle one:       fruitful        void

    2. (1 pts) Is the function catAndPrintTwice() fruitful or void?

      Circle one:       fruitful        void

    3. (1 pts) Suppose these function definitions are loaded into Python. There are two transcripts below, one of which is real, and the other is total fiction.

      Which one shows how Python really works? Circle one:       version 1        version 2

      version 1
      version 2
      >>> printTwice("Gauchos")
      Gauchos
      Gauchos
      >>> msg
      'Gauchos'
      >>>
      >>> printTwice("Gauchos")
      Gauchos
      Gauchos
      >>> msg
      
      Traceback (most recent call last):
        File "", line 1, in 
          msg
      NameError: name 'msg' is not defined
      >>>          
    4. (2 pts) Now explain briefly why the version you chose is the correct one.
      (One or two sentences is enough—keep your answer consise and accurate.)




  2. Suppose we have the following function definitions:

        
    def doubleMsg(msg):
    twice = msg + "\n" + msg return twice def printDoubleMsg(part1, part2): cat = part1 + part2 print doubleMsg(cat)
    return

    1. (1 pts) Is the function doubleMsg() fruitful or void?

      Circle one:       fruitful        void

    2. (1 pts) Is the function printDoubleMsg() fruitful or void?

      Circle one:       fruitful        void

    3. (3 pts) Suppose these function definitions are loaded into Python. There are two transcripts below, one of which is real, and the other two are total fiction.

      Which one shows how Python really works? Circle one:     version 1     version 2     version 3

      version 1
      version 2
      version 3
               
      
      >>> x = doubleMsg("Gauchos")
      >>> printDoubleMsg("Go","Gauchos")
      GoGauchos
      GoGauchos >>> part2 Traceback (most recent call last): File "", line 1, in part2 NameError: name 'part2' is not defined >>> cat 'GoGauchos\nGoGauchos'
      >>>
               
      
      >>> x = doubleMsg("Gauchos")
      >>> printDoubleMsg("Go","Gauchos")
      GoGauchos
      GoGauchos
      >>> part1
      
      Traceback (most recent call last):
        File "", line 1, in 
          part1
      NameError: name 'part1' is not defined
      >>> x
      'Gauchos\nGauchos'
      >>>      
               
               
      >>> x = doubleMsg("Gauchos")
      Gauchos
      Gauchos >>> printDoubleMsg("Go","Gauchos") GoGauchos GoGauchos >>> part1 'Go' >>> x Traceback (most recent call last): File "", line 1, in part1 NameError: name 'x' is not defined >>>

(end, ex13)