【问题标题】:Function using turtle graphics causing errors使用海龟图形的函数导致错误
【发布时间】:2020-06-12 05:23:51
【问题描述】:

我应该创建一个 python 代码来获取列表中的项目并将它们变成条形图。在我的代码中,我有一个 create graph 方法,它在我第一次运行我的代码时起作用,但是当我选择输入不同的数据集时,它会导致错误,特别是在 while 循环中调用 creategraph 时。

def creategraph():
bar = turtle.Turtle()
bar.speed(100)
bar.color("black")
bar.fillcolor("green")
bar.pensize(3)
bar.setposition(-200, 0)

xs = [top(text), eighties(text), seventies(text), sixties(text), belowsixties(text)]
#creates list withthe heights ^^

for i in xs:
    draw_bar(bar, i)
bar.penup()
bar.setposition(-200, -50)
bar.forward(40)
bar.write(str("90s"))
bar.forward(40)
bar.forward(40)
bar.write(str("80s"))
bar.forward(40)
bar.forward(40)
bar.write(str("70s"))
bar.forward(40)
bar.forward(40)
bar.write(str("60s"))
bar.forward(40)
bar.forward(40)
bar.write(str("<60"))
bar.forward(40)

bar.hideturtle()

turtle.done()

这会导致我收到如下错误消息:

exception was: 
    Traceback (most recent call last):
  File "C:\Users\bwoo2\OneDrive\Desktop\CS\Python\__Project2_ScoreAnalysis_Bwoo.py", line 160, in <module>
    creategraph()
  File "C:\Users\bwoo2\OneDrive\Desktop\CS\Python\__Project2_ScoreAnalysis_Bwoo.py", line 102, in creategraph
    bar = turtle.Turtle()
  File "C:\Users\bwoo2\AppData\Local\Programs\Thonny\lib\turtle.py", line 3816, in __init__
    visible=visible)
  File "C:\Users\bwoo2\AppData\Local\Programs\Thonny\lib\turtle.py", line 2557, in __init__
    self._update()
  File "C:\Users\bwoo2\AppData\Local\Programs\Thonny\lib\turtle.py", line 2660, in _update
    self._update_data()
  File "C:\Users\bwoo2\AppData\Local\Programs\Thonny\lib\turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "C:\Users\bwoo2\AppData\Local\Programs\Thonny\lib\turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator

我调用图表的循环是

codebreaker = 0

当密码破解器 == 0 时: 断路器 = 假

text = input("Please enter the file name: ")
f = open(text)

lineList = [line.rstrip('\n') for line in f]

f.close()

text = lineList

print("Results for", text[0])
print("Number of scores:", findscores(text))
print("High score:", findmax(text))
print("Low score:", findmin(text))
print("Avg:", findavg(text))
print("Check other screen for graph of data")
print("Make sure to close out of graph before processing another file")
creategraph()
print("")
y_n = input("Process another file? y/n :")


if y_n == "y":
    breaker = False
elif y_n == "n":
    breaker = True

if breaker == True:
    codebreaker = 1

知道 creategraph 函数有什么问题吗?

【问题讨论】:

    标签: python function error-handling turtle-graphics


    【解决方案1】:

    您的主要问题是turtle.done() 函数仅被调用一次,当您将控制权移交给事件处理程序并且将来与代码的所有交互都将通过事件进行时(鼠标点击、按键等)如果您多次调用creategraph(),则turtle.done() 没有任何业务被调用。

    您的次要问题是您试图将控制台输入与海龟事件结合起来。你真的应该只使用海龟的事件模型。也就是说,您应该使用 turtle 的 textinput() 方法而不是 input() 来弹出基于事件的对话框(即按下“新数据”伪按钮)。

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 2019-04-21
      • 2020-05-11
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多