Iterations Flashcards
1
Q
The factorial of a positive integer is that integer, multiplied by all positive integers that are lower (excluding zero). You write the factorial as the number with an exclamation mark after it. E.g., the factorial of 5 is 5! = 5 * 4 * 3 * 2 * 1 = 120. Write a function that calculates the factorial of its (integer) parameter. Test your function for different parameter values, but do not use very large numbers as factorials grow exponentially. Hint: to do this with a while loop, you need at least one more variable.
A
def factorial(number): i = 0 total = number while i < number: total *= (number -1) number -= 1 i += 1
return total
print(factorial(5))
2
Q
loop control statement
A
else, break, continue