python-3.x - 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
解决方案
问题出在这一行:
return int(birth=20 - age)
int()
是一个接受参数的方法。你给它一个命名的参数birth
,但这不是它所期望的。删除名称,让您更接近:
return int(20 - age)
不幸age
的是不是一个int。我想你想要的是这样的:
return 20 - int(age)
int()
然后,您可以在几行之后删除呼叫。
推荐阅读
- arrays - 在数组结构内调用 init
- django - 如何通过模型显示来自多对多的字段值
- python - 从文件读取时删除格式
- sql - 何时将 JOIN 操作从数据库层移动到服务层?
- flutter - Flutter - 图像在 FlatButton 内根本不显示
- r - 如何在 R 中定义一个新的类方法?
- python - PowerBI - Pandas isnull().sum() 索引
- ios - 使用 SwiftUI 按钮打开 pdf
- typescript - 给定 db 键成员,从实体中提取 db 键类型
- vba - MS Access 2019:如何在更新前检查重复记录,如果发现重复则运行特定程序?