Where we are and what's coming next :
Continue our discussion of functions.
Any questions?
In class, write a python program that prints the "happy birthday" song ... in several iterations.
Then check out 99-bottles-of-beer.net ...
Finish the following example in class.
"""
factorial.py
Looking at functions, doctests, and test-driven deveopment.
"""
def factorial(n):
""" Return the factorial of n, i.e. n!, which is n * (n-1) * ... 2 * 1
>>> factorial(2)
2
>>> factorial(5)
120
"""
return 0 # This is the wrong result. Tests will fail.
def main():
print "-- factorial --"
n = int(input("What is n? (i.e. 5) "))
print("{}! is {}.".format(n, factorial(n)))
if __name__ == "__main__":
import doctest
doctest.testmod()
main()
Discuss the difference between the following two programs.
# Version 1 : what will be printed ?
name = "Jim"
def f():
name = "Joe"
return 1
def main():
x = f()
print(" name is {}".format(name))
main()
# Version 2 : what will be printed ?
names = ["Jim", "John", "Mary"]
def f():
names[0] = "Joe"
return 1
def main():
x = f()
print(" names is {}".format(names))
main()
If there's time left, start discussing conditionals ... that's coming next. :)
last modified | size | ||
birthday_1.py | Sat Dec 21 2024 06:25 pm | 277B | |
birthday_2.py | Sat Dec 21 2024 06:25 pm | 372B | |
birthday_3.py | Sat Dec 21 2024 06:25 pm | 679B | |
factorial.py | Sat Dec 21 2024 06:25 pm | 693B |