Functions and Scope
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
1) Fun
For each of the following programs, enter the result of the print
statement after executing the program.
If evaluating the print
statement in question would cause an error,
write error in the box.
x = 4
def fun(x):
x = 1
return x
y = 5
z = fun(y)
print([x, y, z])
def fun(x):
y[0] += 1
return x
y = [1, 2, 3]
x = [0]
z = fun(y)
print([x, y, z])
x = 5.5
y = 7.5
def fun(y):
y += 2
return bar(y)
def bar(x):
x = x * 3
return x
z = fun(10)
print([x, y, z])
2) Transcript
Below is a single Python program, split up into several pieces for viewing. After each print
statement, provide the value that will be printed to the
screen.
If evaluating the print
statement in question would cause an error,
write error in the box.
x = 14
y = 22
a = 12
b = 7
def f(x):
return x + a
def f2(x):
return y + b
a = 13
print(f(1))
print(f(a))
print(f2(a))
def f3(x, y):
a = x + y
return a + x + y
print(f3(43, 2))
print(a)
Next Exercise: Hip to Be Square