I have a list of years (ranging from 2023 to 1956), and another list of numbers corresponding to each year (2023 -> 1, 2022 -> 5, etc). What I want to do is ask the user to input a certain year, then print the number corresponding to that year. I used a piece of code from this thread, slightly adjusted to fit the lists and data that I have;
inputyear = input("Data?")
for i in range (len(host)):
if host[i][0] == inputyear:
print(inputyear, str(host[i]))
("host" is the list of numbers, btw)
However, when I run this, I get an error stating that "'int' object is not callable," even though neither inputyear
nor input
are (as far as I can tell) integers, and the code seemed to work for the people in aforementioned question thread.
What am I doing wrong here, and how do I fix it?
Thanks in advance for the help.
Referencing the noted issue link and making an assumption about the structure of your "host" list (aka using some artistic license), following is a refactored version of your code with a subset list which seemed to provide the desired output.
host = [('2023', '1'), ('2022', '5')] # Assumed structure of list
inputyear = input("Data? ")
for i in range (len(host)):
if host[i][0] == inputyear:
print(inputyear, str(host[i][1])) # Added second element
The key elements are the structure of the list which is a "year/number" pair and the addition of selecting the second data element within the "str" call.
Following was the terminal output of a quick test referencing the year "2022".
craig@Vera:~/Python_Programs/Year$ python3 Search.py
Data? 2022
2022 5
See if that addresses your requirements.