Reputation: 1
The problem is my program freezes and I can't find what's wrong: I've being re-reading the code over and over, I've searched here and I've also googled and couldn't find a reason. I've seen a theme where the program of one coder froze because of an infinite loop and looked for it in my code but didn't see one. Here is the code below. Thank you for your time!
P.S. I've read the recommendation on less code in the question but it seemed to me that in my case every part is important, maybe that's because I'm a newbie yet.
First, I import the csv file with the names of the states. It has 50 states and it looks like this:
state,x,y
Alabama,139,-77
Alaska,-204,-170
Arizona,-203,-40
etc...
Now this is the code of my program:
import turtle
import pandas
screen = turtle.Screen()
screen.title("U.S. States Game")
# This belowe is the image with the blank_country where my guessed states names will be viewed. It use it as a background.
image = "blank_states_img.gif"
screen.addshape(image)
Tim = turtle.Turtle()
Tim.hideturtle()
Tim.penup()
turtle.shape(image)
print(screen.screensize())
states_data = pandas.read_csv("50_states.csv")
states_list = states_data.state.to_list()
number_of_states = len(states_list)
guessed_states = []
number_of_guessed_states = len(guessed_states)
answer_state = screen.textinput(title="Guess the state", prompt="What's another state's name?").title()
print(answer_state)
while number_of_guessed_states < 50:
if answer_state == "Exit":
missing_states = []
for state in states_list:
if state not in guessed_states:
missing_states.append(state)
new_data = pandas.DataFrame(missing_states)
new_data.to_csv("states_to_learn.csv")
break
if answer_state in states_list:
guessed_states.append(answer_state)
current_state = states_data[states_data.state == answer_state]
Tim.goto(int(current_state.x) - 15, int(current_state.y))
Tim.pendown()
Tim.write(answer_state, align="left", font=("Arial", 6, "bold"))
Tim.penup()
answer_state = screen.textinput(title=f"{number_of_guessed_states}/{number_of_states} states correct",
prompt="What's another state's name?").title()
states_list.pop(states_list.index(answer_state))
if number_of_guessed_states == number_of_states:
Tim.goto(-30, 0)
Tim.pendown()
Tim.write("Congratulations. This doesn't make you smart, but you've guessed all the states and won.",
font=("Arial", 10, "normal"))
Upvotes: 0
Views: 154
Reputation: 41852
One problem with your code is you lock in the value of number_of_guessed_states
before the loop and never update it:
number_of_guessed_states = len(guessed_states)
so even though guessed_states
is changing, number_of_guessed_states
isn't. Also, this seems an odd choice:
screen.addshape(image)
...
turtle.shape(image)
vs. doing:
screen.bgpic(image)
Duplicating this prompt:
answer_state = screen.textinput(...).title()
seems error prone vs. having it once at the top of the loop. For example, here:
answer_state = screen.textinput(...).title()
states_list.pop(states_list.index(answer_state))
You remove the current answer but never removed the answer from before the loop. See if the following rework of your code runs as you intended:
from turtle import Screen, Turtle
import pandas
# This belowe is the image with the blank_country where my guessed states names will be viewed. It use it as a background.
image = "blank_states_img.gif"
screen = Screen()
screen.title("U.S. States Game")
screen.bgpic(image)
turtle = Turtle()
turtle.hideturtle()
turtle.penup()
states_data = pandas.read_csv("50_states.csv")
states_list = states_data.state.to_list()
number_of_states = len(states_list)
guessed_states = []
while len(guessed_states) < number_of_states:
answer_state = screen.textinput(title=f"{len(guessed_states)}/{number_of_states} states correct", prompt="What's another state's name?").title()
if answer_state == 'Exit':
missing_states = []
for state in states_list:
if state not in guessed_states:
missing_states.append(state)
new_data = pandas.DataFrame(missing_states)
new_data.to_csv("states_to_learn.csv")
screen.bye()
if answer_state in states_list:
guessed_states.append(states_list.pop(states_list.index(answer_state)))
current_state = states_data[states_data.state == answer_state]
turtle.goto(int(current_state.x) - 15, int(current_state.y))
turtle.write(answer_state, align='left', font=('Arial', 6, 'bold'))
if len(guessed_states) == number_of_states:
turtle.goto(-30, 0)
turtle.write("Congratulations. This doesn't make you smart, but you've guessed all the states and won.", font=('Arial', 10, 'normal'))
screen.mainloop()
Upvotes: 1