没注释的源代码
factorial = 1
number = int(input("请输入你计算阶乘的数字:"))
if number < 0:
print("{}! 没有阶乘".format(number))
elif number == 0:
print("{}! 等于1".format(number))
else:
for i in range(1, number + 1):
factorial *= i
print("{}! 等于{}".format(number, factorial))
注释的源代码
factorial = 1 #设初值为1
number = int(input("请输入你计算阶乘的数字:"))
if number < 0: #判断是否为负数,负数没有阶乘
print("{}! 没有阶乘".format(number))
elif number == 0: #0的阶乘为1
print("{}! 等于1".format(number))
else:
for i in range(1, number + 1): #for循环求阶乘
factorial *= i
print("{}! 等于{}".format(number, factorial))