这是我的代码:def menu():
print ("Would you like to find the factorial of this number using \n Option 1: sequential programming? \n Option 2: recurion?")
choice = input("Enter the number of your preferred option: ")
if (choice >= 1) and (choice <= 2):
return choice
else:
print ("That is not an option.")
def recur_factorial(n):
if n == 1 or n == 0:
return 1
else:
fact = (n) * (recur_factorial(n - 1))
print fact
return
def main():
number = int(input("Enter a positive integer: "))
if number < 0:
print ("Sorry, factorial does not exist for negative numbers.")
else:
choice = menu()
if choice == 1:
if number == 0:
print 1
else:
for i in range(1,number + 1):
answer = factorial * (i)
print answer
else:
recur_factorial(number)
main()
我试着用两种不同的方法得到数的阶乘。选项1应使用顺序编程。当我现在使用选项1时,我想到了错误:全局名称'factorial'未定义。当我使用选项2(应该是递归)时,我得到错误:不支持*:“int”和“NoneType”的操作数类型。关于如何解决这个问题有什么建议吗?我使用的是Python语言。在
我收到的选项1的完整错误:
^{pr2}$
我收到的选项2的完整错误:Traceback (most recent call last):
File "/Users/user/Documents/project4", line 33, in
main()
File "/Users/user/Documents/project4", line 31, in main
recur_factorial(number)
File "/Users/user/Documents/project4", line 13, in recur_factorial
fact = (n) * (recur_factorial(n - 1))
File "/Users/user/Documents/project4", line 13, in recur_factorial
fact = (n) * (recur_factorial(n - 1))
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'