Getting turtle to draw a line

I have code where I type “turtle.pendown()” then “turtle.forward(50)”

but to get it to draw a line I have to follow it with “turtle.dot(20,“White”)” - “turtle.done()” won’t do.

Previously in the code the turtle draws lines OK so something must switch it off.

What would, apart from “turtle.penup()”

We need to see the code and possible errors if you want help.

Yes - I had better put up the whole code as I don’t know where the fault is. Meanwhile, I have been getting my head round turtle heading increasing anticlockwise - perhaps the author was left-handed.

In this code, I don’t know why line 121: turtle.dot(20,“white” should be necessary. The code follows, I hope:

python
# CODE follows
# import package
import turtle
from time import sleep # to allow test delays

# This script seems to set up window and screen size OK.
# set screen and drawing remain as it is.
# turtle.screensize(canvwidth=700, canvheight=400,
#                   bg="blue")

# Trying "setup" instead - screensize does not change window size

turtle.tracer(0) # in prev programs speeded everything up
sc = turtle.Screen()
sc.setup(width=800, height=600) # units for dimensions smaller than with canvwidth etc.
# color cannot be set from setup so:
sc.bgcolor("white")

def Vec2D_to_tuple(mypos):
    """
    I do not understand the difference between Vec2D "type" and a 2-element tuple.
    That at about line 108 "forward" does not draw a line unless I add a subsequent dot
    may have something do with this. I have no idea.
    """
    myX = mypos[0]
    myRX = round(myX, 2)
    myY = mypos[1]
    myRY = round(myY, 2)
    myDuple = (myRX, myRY)
    return myDuple


def fence_gap(skew):
    turtle.seth(-skew)
    turtle.forward(5) # this will be repeated at end
    turtle.seth(270) # down-facing
    turtle.backward(10) # for top of post
    turtle.forward(10)
    turtle.forward(50) # guess
    turtle.backward(10)
    turtle.seth(180 - skew) # west and a bit up
    turtle.forward(5) # guess
    turtle.seth(90) # up-facing
    turtle.backward(10) # for bottom of post
    turtle.forward(10)
    turtle.forward(50) # same guess as first
    turtle.backward(10)
    turtle.seth(-skew) # east (0) and a bit down
    turtle.forward(5) # back to start

turtle.width(1)
turtle.speed(10)


for n1 in range(20):
    fence_gap(15)

for n2 in range(20):
    fence_gap(25)

for n3 in range(20):
    fence_gap(35)

for n4 in range(20):
    fence_gap(45)

turtle.penup() # These 3 lines replacing teleport in continuing search
turtle.goto(-20,20)
turtle.pendown()
# turtle.teleport(-20,20) # commented-out teleport
# turtle.dot(20, "red") # after fence


with turtle.fill(): # column of lamp
    turtle.seth(0) # east
    turtle.forward(20)
    turtle.seth(270) # south
    turtle.forward(80)
    turtle.seth(180) # west
    turtle.forward(20)
    turtle.seth(90) # north
    turtle.forward(80) # now at northwest corner of base
    turtle.seth(0)
    turtle.forward(2)
    turtle.seth(88) # north, slightly east
    turtle.forward(200)
    turtle.seth(0)
    turtle.forward(2)
    topRightV2D = turtle.pos() # position at the top right
    # as Vec2D - which is immutable - 2-dimensional class.
    topRight = Vec2D_to_tuple(topRightV2D)
    turtle.seth(272) # south, slightly east
    turtle.forward(200)

# At this point the column for the lamp has been drawn.
# Next to draw the small crossbar (?function)
# then the lantern.

#---------------------------------------
# topRight = (-9.02, 219.88) # temporary

turtle.penup()
turtle.setpos(topRight)
turtle.seth(180) # West
turtle.fd(1) # to middle of top
# turtle.dot(20,"green") # at middle of top
topOfColumn = turtle.pos() # middle of top
turtle.pendown()
turtle.seth(300) # ought to be southeast
print(turtle.mode())
turtle.forward(50) # Was drawing the line but only drew if followed by dot
# turtle.dot(20,"red")
rt_end_of_crossbar = turtle.pos()
print("end of line is ",turtle.pos())
print("topRight is ",topRight)
turtle.back(50)
turtle.seth(240) # ought to be southwest
# Tried converting the Vec2D value to tuple (topRight) - no difference
# Tried rounding - no difference
# Expressing teleport as penup-goto-pendown did not solve the problem
turtle.forward(60) # 50 not far enough ? why
turtle.dot(20,"white") # kludge to make line appear
# turtle.done() # Tried this to make turtle draw but it didn't

turtle.hideturtle()

turtle.save("fenceAndLamp.ps", overwrite=True) # last arg needed as overwrite is not default.


turtle.Screen().exitonclick() # Note Screen(), parens calling rather than passing Screen
```

Welcome to the joys of debugging!
I have found the answer, but it’s probably a better learning opportunity if you go through the process yourself.

What you do is you disable your program line by line until you’re back to a program that just reads import turtle; turtle.forward(50). At some point you’ll notice that the final line that’s supposed to be drawn re-appears.

I’ve done this for your code, there is a very particular line that causes the problem, and it’s not hard for me to come up with an hypothesis of why this line causes this behaviour. (But only with hindsight, knowing that it is that line causing the problem).

Thanks. I am not a student, but rather a user (up to now).

So I have made the lines reappear by prefacing “turtle.forward(50)” with “turtle.tracer(1)”. I see that the behaviour of this tracer function differs between Python 2 and Python 3. Presumably the dot function forces an update which is why the lines were appearing, but this is better. I am not sure whether I should label this as “the solution” - there could be a better option I don’t know about.

Yes, so the relevant documetation is here: https://docs.python.org/3/library/turtle.html#turtle.tracer

turtle.tracer controls the animation. You’ll notice that turtle.tracer(0) actually results in undocumented behaviour, because the documentation describes

def turtle.tracer(n=None, delay=None):
    """Parameters:
            n – nonnegative integer
            delay – nonnegative integer
    """

The effect of this undocumented behaviour is appearently to turn off automatic screen updates.

The “Animation Control” section of the documentation gives you several options to turn off animation whilst still being able to see the picture at the end. You can use with turtle.no_animation(), or you can call turtle.update() at the end of your script.

(“turning off animation” is what you described in your code comments as “this makes the script go faster”)