ich versucht habe, mein Programm zu laufen, aber jedes Mal, wenn ich tue, bekomme ich diese:Python KeyError, aber ohne dict
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)
Ich weiß nicht, was es bedeutet, durch Schlüsselfehler in diesem Fall weil ich im Gegensatz zu vielen anderen Leuten kein Diktat benutzt habe. Mein Programm soll die Schildkröte nach 50 Zügen zur Mitte zurückkehren lassen, aber nicht 0,0 in der eingestellten Badpos zählen. Mein Programm ist:
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()
Die [set] (https://docs.python.org/3/library/stdtypes.html#set.remove) Dokumentation besagt, dass '() entfernen' wirft eine 'KeyError' wenn das Ding Sie versuchen, zu entfernen ist nicht da. – JETM