简体   繁体   中英

I cannot figure out why my output is being printed twice

I have a script that has 2 functions. The first function calculates the factorial of a number passed into it, the second function takes an integer and prints out all of the factorials up to that number. It is calculating the factorials correctly and printing the set correctly but it prints the set twice which has me confused.

Here is the code I have and I want it to print just 1 - 362880 once but it prints twice as shown below. Also the "None" in there does not make sense to me. Any ideas on this?

Code:

#calculate factorial of a number
def factorial(n):
    #initialize and assign product to be returned to the print function
    product = 1
    for i in range(1, n + 1):
        product = product * i
    return product

#print factorials up to 1 less than given argument
def print_factorial(n):
    for i in range(n):
        print(factorial(i))

print_factorial(10)

Result: 1 1 2 6 24 120 720 5040 40320 362880 1 1 2 6 24 120 720 5040 40320 362880 None

Both factorial(0) and factorial(1) returns 1 , so 1 will be printed twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM