pythonpandascsv

Checking that the user's answer is one of the states


I am trying to create a game where the user see a map of the US and then enters the states one by one and if they get it correct the name will move on top of the state. Currently, I am getting the error TypeError: argument of type method is not iterable. I believe that the error is coming from the separate csv file with all of the states inside it:

state,x,y
Alabama,139,-77
Alaska,-204,-170
Arizona,-203,-40
Arkansas,57,-53
California,-297,13
Colorado,-112,20
Connecticut,297,96
Delaware,275,42
Florida,220,-145
Georgia,182,-75
Hawaii,-317,-143
Idaho,-216,122
Illinois,95,37
Indiana,133,39
Iowa,38,65
Kansas,-17,5
Kentucky,149,1
Louisiana,59,-114
Maine,319,164
Maryland,288,27
Massachusetts,312,112
Michigan,148,101
Minnesota,23,135
Mississippi,94,-78
Missouri,49,6
Montana,-141,150
Nebraska,-61,66
Nevada,-257,56
New Hampshire,302,127
New Jersey,282,65
New Mexico,-128,-43
New York,236,104
North Carolina,239,-22
North Dakota,-44,158
Ohio,176,52
Oklahoma,-8,-41
Oregon,-278,138
Pennsylvania,238,72
Rhode Island,318,94
South Carolina,218,-51
South Dakota,-44,109
Tennessee,131,-34
Texas,-38,-106
Utah,-189,34
Vermont,282,154
Virginia,234,12
Washington,-257,193
West Virginia,200,20
Wisconsin,83,113
Wyoming,-134,90

But I am not entirely sure. In terms of the main code in my program I have this:

from turtle import Turtle, Screen
import pandas

turtle = Turtle()
screen = Screen()
screen.title("U.S State Game Guesser")
image = "blank_states_img.gif"
screen.addshape(image)
screen.setup(750, 500)

turtle.shape(image)

data = pandas.read_csv("50_states.csv")
states = data.state.to_list

user_answer = screen.textinput(title="Guess the state", prompt="What's the states name?").title()

if user_answer in states:
    t = Turtle()
    t.hideturtle()
    t.penup()
    state_data = data[data.state == user_answer]
    t.goto(int(state_data.x, state_data.y))
    t.write(state_data.state)
else:
    pass

screen.mainloop()

Can anyone give me a little help resolving this issue as I cannot find anyone else who's had this same issue?


Solution

  • The error is here:

    states = data.state.to_list
    

    It should be:

    states = data.state.to_list()
    

    When calling a function or method, you need to include () even if there are no parameters to pass. You already have some examples of this in your code:

    turtle = Turtle()
    screen = Screen()
    screen.mainloop()
    

    If you forget this, the function or method itself is referred to leading to type errors