A multiple-choice question to distinguish novices from competent practitioners:
y = 3
def a(y):
if y > 0:
return y + a(y - 1)
return y
print a(y)
Without resorting to the Python interpreter, what do you think this program will print out when run?
- 6
- 3
- An exception will be thrown
A “multiple-choice question to tell whether your audience has actually learned what you set out to teach”
x = 1
def fun():
print ''x is: %i'' % x
x = 3
x = 2
fun()
Without resorting to the Python interpreter, what do you think this program will print out when run?
- ‘x is: 1′
- ‘x is: 2′
- An exception will be thrown
- ‘x is: 3′
A “short practical exercise they could do to exercise their new knowledge”
The following code is an attempt to calculate n! (n! = n*(n-1)*(n-2)*…*2*1) for n = 4.
However, it is broken. Your challenge, should you choose to accept it, is to not only fix it, but also to make it work for different values of n.
n = 4
def factorial():
number = number - 1
if number == 0:
return 1
else:
return number * factorial()
print '%i' % factorial()