Python, user input as a function argument not working: TypeError: 'str' object is not callable

Python, user input as a function argument not working: TypeError: 'str' object is not callable

我昨天才开始学习Python,所以这可能是个愚蠢的问题。尽管如此,我一直在寻找解决方案一段时间但找不到! 这是我最基本的程序:

def year(year):
  try:
    print(int(year))
  except:
    print("you did not enter a year")


if __name__ == "__main__":
  year = input("what year? ")
  year(year)

问题是,当我在终端中 运行 它时,我收到这条消息:

Traceback (most recent call last):
  File "fonctions.py", line 24, in <module>
    year(year)
TypeError: 'str' object is not callable

有人可以帮我解决这个问题吗? 谢谢! 奥利维尔

更改输入变量名称,因为这里 python 解释它正在调用 year 变量,而不是函数:

if __name__ == "__main__":
  year_val = input("what year? ")
  year(year_val)