简体   繁体   中英

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

I just started learning Python yesterday, so this is probably a stupid question. Nonetheless I have been searching for the solution for a while and couldn't find it: Here is my very basic program:

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


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

The thing is that, when I am running it in the terminal I get this message:

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

Could someone help me sort this out? Thank you! Olivier

Change the input variable name, because in here python is interpreting that it is calling the year variable, not the function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM