简体   繁体   中英

I got an error in the penup() function of the turtle module

Here's the error message I got:

Traceback (most recent call last):
  File "\\tsclient\E\Python34\Doc\mazes5.py", line 62, in <module>
    sky.penup()
  File "C:\Python34\lib\turtle.py", line 2107, in penup
    self.pen(pendown=False)
  File "C:\Python34\lib\turtle.py", line 2425, in pen
    self._newLine()
  File "C:\Python34\lib\turtle.py", line 3287, in _newLine
    self._pencolor, self._pensize)
  File "C:\Python34\lib\turtle.py", line 545, in _drawline
    self.cv.coords(lineitem, *cl)
  File "<string>", line 1, in coords
  File "C:\Python34\lib\tkinter\__init__.py", line 2307, in coords
    self.tk.call((self._w, 'coords') + args))]
  File "C:\Python34\lib\tkinter\__init__.py", line 2305, in <listcomp>
    return [getdouble(x) for x in
ValueError: could not convert string to float: 'floating'

I don't know how I'm meant to fix this, as penup() is a function that requires no input. Also, my maze generator drawer got all frazzled and drew in directions I never said it could draw. This has happened multiple times.

Here is a screenshot of what it generated before crashing. I told it to rotate in increments of 270 degrees.

在此处输入图片说明

As requested, here is my code. Be warned, I have no clue where the error is, so I'm going to paste the entire thing. The problematic lines are towards the bottom.

import turtle, random
from turtle import *
maze=[]
mazelev=[]
x=int(input("How many columns?\n"))
y=int(input("How many rows?\n"))
siz=int(input("How wide should the passages be?\n"))
maze=[[5]*x for n in range(y)]
wn=turtle.Screen()
wn.setup(2000, 1000, 0, 0)
sky=turtle.Turtle()
sky.speed(0)
wn.delay(0)
sky.goto(-700, -350)
wn.bgcolor("black")
sky.color("white")
sky.pensize(1)
n=0
tot=0
for i in range(x+1):
    cx=sky.xcor()
    cy=sky.ycor()
    sky.pendown()
    sky.goto(cx, cy+(siz*y))
    sky.penup()
    sky.goto(cx+siz, -350)
sky.goto(-700,-350)
for i in range(y+1):
    cx=sky.xcor()
    cy=sky.ycor()
    sky.pendown()
    sky.goto(cx+(siz*x), cy)
    sky.penup()
    sky.goto(-700, cy+siz)
sky.pensize(siz-1)
sky.color("red")
sky.speed(0)
turtle.tracer(0,0)
num=0
eks=0
why=0
direc=1
sky.goto(-700+(.5*siz),-350+(.5*siz))
for i in range(y):
    for j in range(x):
        eks=-700+(.5*siz)+siz*j
        why=-350+(.5*siz)+siz*i
        sky.goto(eks, why) #second error (when first error is made a comment)
        direc=(direc+siz-1)
        direc=direc//1
        direc=direc*270
        sky.seth(direc)
        sky.pendown()
        sky.forward(siz) #third error
        sky.penup() #first error
        direc=direc/90
    wn.update() #fourth error
sky.goto(-700+(.5*siz),-350-(.5*siz)+siz*y)
sky.pendown()
sky.goto(-700+(.5*siz)+(siz*x),-350-(.5*siz)+siz*y)
sky.penup()
sky.hideturtle()
wn.update()
print("done")

Your error comes from the fact that direc becomes infinite. I frankly don't understand what that variable is supposed to represent, but you seem to add siz-1 to it on each iteration, then effectively multiply it by 3 (by first multiplying by 270 , then later dividing by 90 ). This means it grows exponentially, and since it becomes a floating point number when you do the division, it eventually overflows to infinity when the float data type can't represent it any longer.

Python's turtle module seems to behave somewhat badly when given infinite values, but that doesn't mean it's not a bug in your own code.

If direc is supposed to be an angle, you might want to do your math on it modulo 360. That will at least prevent it from growing uncontrollably (though it may not make your code make any sense).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM