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.
Suppose we have the following function definitions:
def printTwice(msg):
print msg
print msg
def catAndPrintTwice(part1, part2):
cat = part1 + part2
printTwice(cat)
printTwice() fruitful or void?catAndPrintTwice() fruitful or void?version 1 |
version 2 |
>>> printTwice("Gauchos")
Gauchos
Gauchos
>>> msg
'Gauchos' |
>>> printTwice("Gauchos")
Gauchos
Gauchos
>>> msg
Traceback (most recent call last):
File "", line 1, in |
(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.)
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
doubleMsg() fruitful or void?printDoubleMsg() fruitful or void?version 1 |
version 2 |
version 3 |
>>> x = doubleMsg("Gauchos")
>>> printDoubleMsg("Go","Gauchos")
GoGauchos |
>>> x = doubleMsg("Gauchos")
>>> printDoubleMsg("Go","Gauchos")
GoGauchos
GoGauchos
>>> part1
Traceback (most recent call last):
File "", line 1, in |
>>> x = doubleMsg("Gauchos")
Gauchos |
(end, ex13)