Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Imperative Programming
Introduction to Computer Science cs105
Haverford College
# what film?
# returns base^exp
def power(base, exp):
precondition((type(base) == type(2)) or (type(base) == type(2.2)))
precondition(type(exp) == type(2))
precondition(exp >= 0)
answer = base
while exp > 1:
exp = exp - 1
answer = answer * base
return answer
# returns the original string backwards
def reverse(s):
precondition(type(s) == type("some string"))
inverted_s = ""
while len(s) > 0:
inverted_s = inverted_s + s[-1]
s = s[:-1]
return inverted_s
if __name__ == "__main__":
import doctest
doctest.testmod()
print "looping done"