如何使用嵌套for循环以相反的顺序打印

时间:2016-01-25 06:48:12

标签: python

numline = [1,2,3,4,5,6,7,8,9,10]
factorial = 1
for n in numline:
        factorial = factorial * n  
        print str(n) + "!: " + str(factorial)

如何使用嵌套for循环以相反的顺序打印此解决方案? 所以,从10开始!到1!

1 个答案:

答案 0 :(得分:3)

您需要将结果存储在列表中,然后以相反的顺序输出。

factorial = 1
results = []
for n in range(1,11):
    factorial = factorial * n
    results.append(factorial)
for i, item in enumerate(results[::-1]):
    print "{}! = {}".format(10-i, item)

输出:

10! = 3628800
9! = 362880
8! = 40320
7! = 5040
6! = 720
5! = 120
4! = 24
3! = 6
2! = 2
1! = 1