Python 3.x int()的关键字参数无效
我尝试过不使用类型,使用float-我的新手犯了什么错误Python 3.x int()的关键字参数无效,python-3.x,types,Python 3.x,Types,我尝试过不使用类型,使用float-我的新手犯了什么错误 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:/DSD
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
你今年几岁?54
回溯(最近一次呼叫最后一次):
文件“D:/DSDJ/My files/ParameterPassReturnTest.py”,第13行,在
出生=智力(年龄)
文件“D:/DSDJ/My files/ParameterPassReturnTest.py”,第8行,Calage
返回整数(出生=20-年龄)
TypeError:“birth”是int()的无效关键字参数
进程已完成,退出代码为1
问题出在这一行:
return int(birth=20 - age)
int()
是一个接受参数的方法。您给它一个命名参数birth
,但这不是它所期望的。删除该名称,以使您更接近:
return int(20 - age)
不幸的是,age
不是整数。我想你想要的是:
return 20 - int(age)
然后,您可以在几行之后删除
int()
调用。int调用是一个函数。通过说“出生=20岁”,你基本上是在说,“出生的论点是(20岁)”。函数正在查找该参数,但找不到它,这就是为什么会出现该错误。照办
def calcAge(age):
return 20 - int(age)
age = int(input("What age will you turn this year? "))
birth = calcAge(age)
哇-以合适的格式添加问题是一个学习曲线,添加评论是一个全新的故事-谢谢你的帮助-现在工作得很好。@GailWittich没问题哇-以合适的格式添加问题是一个学习曲线,添加评论是一个全新的故事-谢谢你的帮助-现在工作得很好。