2017-02-09 115 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