简体   繁体   English

类型错误:+ 不支持的操作数类型:“int”和“NoneType”

[英]TypeError: unsupported operand type(s) for +: 'int' and 'NoneType

I am new user in Python, I have a problem with the None type, I looked different question, but the problem persists.我是 Python 的新用户,我遇到了 None 类型的问题,我看了不同的问题,但问题仍然存在。

my code is calculate factorial我的代码是计算阶乘

def fact(k): 
    if k > 0: 
        result = k + fact(k-1) 
        print(result) 
    else: 
        result=0 
    return result

fact(3)

I tried your code and could not produce that error.我尝试了您的代码,但无法产生该错误。 I think you have some indentation issues where your loop is not following the logic you are expecting.我认为你有一些缩进问题,你的循环不符合你期望的逻辑。 Remember what is in the loop and what is out of the loop is dependent on the white space, so be careful with tabs and spaces.请记住循环中的内容和循环外的内容取决于空格,因此请注意制表符和空格。

Also there are some errors in your logic你的逻辑也有一些错误

  1. you need k * fact(k-1) not +你需要 k * fact(k-1) 而不是 +
  2. the else should be result = 1 else 应该是 result = 1

edited code is this:编辑后的代码是这样的:

def fact(k): 
    if k > 0: 
        result = k * fact(k-1) 
        print(result) 
    else: 
        result=1 
    return result

Also a more Pythonic way to write your function is to have it return the actual result instead of passing the result to a variable and returning the variable.还有一种更 Pythonic 的方式来编写 function 是让它返回实际结果,而不是将结果传递给变量并返回变量。 My assumption is that you're printing the result within the function to trouble shoot as this certainly in your final version would be removed.我的假设是您在 function 中打印结果以排除故障,因为这在您的最终版本中肯定会被删除。 Regardless, try this code instead:无论如何,请尝试以下代码:

def fact_v2(k): 
    if k > 0: 
        return k * fact_v2(k-1) 
    else: 
        return 1 

and you would call you function like this:你会这样称呼你 function :

print(fact_v2(11))

Good luck, Python is a fun language and may seem intimidating at fist, but you'll get there!祝你好运,Python 是一种有趣的语言,乍一看可能令人生畏,但你会成功的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:* 不支持的操作数类型:'NoneType' 和 'int' - TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' TypeError: 不支持的操作数类型 -: 'NoneType' 和 'int' - TypeError: unsupported operand type(s) for -: 'NoneType' and 'int' 类型错误:+= 不支持的操作数类型:'int' 和 'NoneType - TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType 类型错误:% 不支持的操作数类型:'NoneType' 和 'int' - TypeError: unsupported operand type(s) for %: 'NoneType' and 'int' TypeError:-:“ int”和“ NoneType”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'NoneType' TypeError:+不支持的操作数类型:“ int”和“ NoneType” - TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' TypeError:+不支持的操作数类型:“ NoneType”和“ int” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' Python-TypeError:*:'NoneType'和'int'不受支持的操作数类型 - Python - TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' Python-TypeError:+不支持的操作数类型:“ int”和“ NoneType” - Python - TypeError: unsupported operand type(s) for +: 'int' and 'NoneType" TypeError:+不支持的操作数类型:“ int”和“ NoneType”返回长度 - TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' return length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM