2017-02-09 69 views
-1

我一直在试图运行我的程序,但我每次做的时候,我得到这个:的Python KeyError异常,但没有字典

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\GURNHH\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__ 
    return self.func(*args) 
    File "C:\Users\GURNHH\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 686, in eventfun 
    fun() 
    File "C:/Users/GURNHH/OneDrive - Rugby School/ICT/Python/bored.py", line 22, in k1 
    badpos.remove((int(turtle.xcor()), int(turtle.ycor()))) 
KeyError: (0, 0) 

我不知道它在这种情况下是指键错误,因为与许多其他人不同,我没有使用字典。 我的程序应该让乌龟在50次移动后回到中心,但不会在设置的badpos中计数0,0。我的程序是:

from turtle import Turtle, Screen 
    from math import * 
    from random import * 

    random = 0 

    """def add(): 
     random = random + 1 
    def check(): 
     if random > 4: 
      random = 0""" 

    def k1(): 
     global random 
     turtle.forward(10) 
     random = random + 1 

     if random > 5: 
      turtle.goto(0,0) 
      badpos.remove((int(turtle.xcor()), int(turtle.ycor()))) 


     position = (int(turtle.xcor()), int(turtle.ycor())) 

     if position in badpos: 
      turtle.color("red") 
      screen.bye() 


    def k2(): 
     turtle.left(90) 

    def k3(): 
     turtle.right(90) 

    turtle = Turtle(shape="turtle") 

    badpos = set() 

    screen = Screen() 
    screen.setup(1200, 700) 
    screen.title("Turtle-Snaky Thing") 

    screen.onkey(k1, "Up") 
    screen.onkey(k2, "Left") 
    screen.onkey(k3, "Right") 

    screen.listen() 

    screen.mainloop() 
+6

[set](https://docs.python.org/3/library/stdtypes.html#set.remove)文档指出,如果你正在尝试的东西是'remove()'会引发'KeyError'删除不存在。 – JETM

回答

0

令我印象深刻的是如何获取工作代码并将其完全搞乱。这是我原来的例子的返工与50移动约束补充说:

from turtle import Turtle, Screen 

def k1(): 
    global move_count 

    turtle.forward(10) 

    move_count += 1 

    if move_count % 50 == 0: 
     turtle.home() 

    position = (int(turtle.xcor()), int(turtle.ycor())) 

    if position != (0, 0) and position in badpos: 
     turtle.color("red") 
     screen.bye() 

    badpos.add(position) 

def k2(): 
    turtle.left(90) 

def k3(): 
    turtle.right(90) 

turtle = Turtle(shape="turtle") 

move_count = 0 
badpos = set() 

screen = Screen() 
screen.setup(1200, 700) 
screen.title("Turtle-Snakey Thing") 

screen.onkey(k1, "Up") 
screen.onkey(k2, "Left") 
screen.onkey(k3, "Right") 

screen.listen() 

screen.mainloop() 

我被你纳入我的例子批发,但不接受我的答案之前少留下深刻的印象。这种新的变化是棘手使用,因为只有你可以从家里重新启动很多次,在90度角,而不是踩前行,即使排除来源:

enter image description here

也许使转弯60或30度可能会给你更多的出门机会。

PS。你在这个程序中没有使用dict()。这是一个set()

+0

谢谢,我该如何将pdf图片保存为pdf? – Gurneyguy

+0

@Gurneyguy,至少有几种方法。简单的方法是使用一个单独的工具来进行屏幕截图并以您想要的格式保存 - 这就是我通常所做的。在Python中,更棘手的方法是通过'turtle.Screen()。getcanvas()'从屏幕上获取tkinter画布对象,然后调用canvas上的'postscript()'方法来生成PostScript文件。然后,您需要将PostScript文件转换为PDF(它们都是Adobe格式)。请阅读tkinter文档以获取'postscript()'方法参数的详细信息。 – cdlane

+0

你能给我一个代码的例子吗?我刚开始python,所以我不知道我在做什么,我试过这个:从turtle import Turtle,screen screen.onkey(k100,“o”)#save picture def k100(): bob = Turtle ) ts = bob.getscreen() name = input(“你想叫什么名字?”) ts.getcanvas()。postscript(file =(name,'.png'),colormode ='color “) – Gurneyguy