在matlab中如何用递归求阶乘的和,我不知道如何求一个数的阶乘。有谁能帮我在递归和使用顺序编程中做到这一点吗?...

这是我的代码: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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值