Reputation: 33
I'm trying to follow along with the exercises in the "Think Python" book and ran into a problem trying to get turtle to work. Here is the exercise I'm on. (4.1)
The code they want me to execute looks like this:
import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()
bob.fd(100)
bob.lt(90)
bob.fd(100)
But when I execute it, the turtle doesn't move and I get the following error when I close the window:
Traceback (most recent call last):
File "C:\Users\Phil\PycharmProjects\untitled\mypolygon.py", line 6, in <module>
bob.fd(100)
File "C:\Python\3.5\lib\turtle.py", line 1637, in forward
self._go(distance)
File "C:\Python\3.5\lib\turtle.py", line 1605, in _go
self._goto(ende)
File "C:\Python\3.5\lib\turtle.py", line 3158, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Python\3.5\lib\turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Python\3.5\lib\tkinter\__init__.py", line 2308, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".1395635997272"
I'm not sure what the debugger is saying, and I can't really continue until I figure out where I went wrong. Any help would be greatly appreciated.
Upvotes: 1
Views: 384
Reputation: 5082
The bob.fd(100)
line (and all following lines) will never be reached as the turtle.mainloop()
call starts a loop (as its name implies).
Change your code so that it reads:
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
And everything should work.
(Since you are not handling any events in your program, I would recommend not calling turtle.mainloop()
for now; if you do want to follow the book's example, make sure to do as it says: "To draw a right angle, add these lines to the program (after creating bob and before calling mainloop)").
There are similar questions here, here and here.
Upvotes: 2