簡體   English   中英

int() 的無效關鍵字參數

[英]invalid keyword argument for 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不是一個整數。 我想你想要的是這樣的:

return 20 - int(age)

然后,您可以在幾行之后刪除int()調用。

int 調用是 function。 通過說“birth=20-age”,您基本上是在說“參數birth is (20-age)”。 function 正在尋找該參數但找不到它,這就是您收到該錯誤的原因。 做就是了

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM