Ryan
Ryan

Reputation: 59

Python Turtle Graphics Simplification

just reviewing for my upcoming midterm. We were given Past midterm problems but no solutions. I am trying to grasp the knowledge best i can.

For this problem, it asks to define a function named equalSigns, pass it values t and length. So, i just need to make my program in turtle graphics, create two parellel line, simple enough i suppose. this is my code that i wrote just for it to correctly output an equal sign of x length. (then of course i would convert it to a function) My question, is there any better way to create this?

    import turtle
t=turtle.Turtle()
s=turtle.Screen()

t.forward(200)
t.penup()
t.home()
t.right(90)
t.forward(50)
t.pendown()
t.left(90)
t.forward(200) 
'''i suppose i dont have to go home and then down. 
instead just continue and go down and forward left.
but either way, is this the best approach to take?
'''

Upvotes: 0

Views: 602

Answers (2)

cdlane
cdlane

Reputation: 41872

Perhaps you could have your turtle think outside the shell:

import turtle
import tkinter as _

_.ROUND = _.BUTT

turtle.width(50)
turtle.forward(200)
turtle.color("white")
turtle.width(48)
turtle.backward(200)

turtle.done()

enter image description here

(The vertical gray bars at the two ends are artifacts of GIF conversion and not present when the program is run.)

Upvotes: 0

Prune
Prune

Reputation: 77837

Yes, I think there's a better way. Most of all, I think you turned the wrong way: you need to make a second right turn to come back along the lower line.

You could make a routine that does a half-equals, and then all it twice to get the two lines. Think of this as drawing a rectangle, except that the short sides are invisible.

# Draw long side
t.pendown()
t.forward(x)
t.penup()
t.right(90)

# Move along short side without drawing
t.forward(x/4)
t.right(90)

That gets you to the opposite corner of the rectangle. Call this twice, and you're done ... and back at the starting point.

Upvotes: 2

Related Questions