Python程序冻结(使用熊猫和乌龟模块)

发布于 2025-01-28 03:50:18 字数 2201 浏览 3 评论 0原文

问题是我的程序冻结了,我找不到问题:我正在一遍又一遍地重新阅读代码,我在这里搜索了,我也搜索过搜索,找不到原因。我看到了一个主题,一个编码器的程序由于无限循环而冻结,并在我的代码中寻找但没有看到它。这是下面的代码。谢谢您的宝贵时间!

ps我已经阅读了有关问题少的代码的建议,但是在我看来,每个部分都很重要,也许是因为我是新手。

首先,我将带有状态名称的CSV文件导入。它有50个州,看起来像这样:

state,x,y
Alabama,139,-77
Alaska,-204,-170
Arizona,-203,-40
etc...

现在这是我程序的代码:

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"))

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"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2025-02-04 03:50:18

代码的一个问题是,您锁定number_guessed_states在循环前的值中,也不要对其进行更新:

number_of_guessed_states = len(guessed_states)

因此,即使guesseed_states正在更改,nubme_guess_guessed_states不是。另外,这似乎是一个奇怪的选择:

screen.addshape(image)
...
turtle.shape(image)

vs.做:

screen.bgpic(image)

复制此提示:

answer_state = screen.textinput(...).title()

似乎错误容易出现,而不是将其放在循环的顶部。例如,这里:

answer_state = screen.textinput(...).title()
states_list.pop(states_list.index(answer_state))

您删除当前答案,但从未在循环之前从中删除答案。查看代码的以下返工是否按照您的意图运行:

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文