【发布时间】: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