首页 > 解决方案 > int() 的无效关键字参数

问题描述

我试过不用类型,用浮点数 - 我的新错误是什么?

def calcAge(age):
    return int(birth=20 - age)

age = int(input("What age will you turn this year? "))
birth = int(calcAge(age))

结果:

What age will you turn this year? 54

Traceback (most recent call last):
File "D:/DSDJ/My files/ParameterPassReturnTest.py", line 13, in <module>
  birth = int(calcAge(age))
File "D:/DSDJ/My files/ParameterPassReturnTest.py", line 8, in calcAge
  return int(birth=20 - age)
TypeError: 'birth' is an invalid keyword argument for int()

Process finished with exit code 1

标签: python-3.xtypes

解决方案


问题出在这一行:

return int(birth=20 - age)

int()是一个接受参数的方法。你给它一个命名的参数birth,但这不是它所期望的。删除名称,让您更接近:

return int(20 - age)

不幸age的是不是一个int。我想你想要的是这样的:

return 20 - int(age)

int()然后,您可以在几行之后删除呼叫。


推荐阅读