I'm kind of new to Python, and I'm trying to improve (especially) my object oriented programming, and getting to know Python syntax better.
I'd love to know if there is something to improve, (especially) in the matter of object oriented, or making the code more Pythonic (Syntactic sugar).
import turtle
RIGHT = 'r'
LEFT = 'l'
class Dragon_Curve(turtle.Turtle):
def __init__(self, iterations, length):
super(Dragon_Curve, self).__init__()
self.iterations = iterations
self.length = length
self.hideturtle()
self.speed(50)
self.sequence = self.create_seq("", iterations)
self.draw_sequence(self.sequence)
def draw_sequence(self, dragon_sequence):
for i in dragon_sequence:
if i == RIGHT:
self.right(90)
self.forward(length)
elif i == LEFT:
self.left(90)
self.forward(length)
# Create the dragon's path, depending on the number of iterations.
def create_seq(self, given_seq, num_iterations):
self.seq = ""
if num_iterations == 0:
print("the dragon's path is {}".format(given_seq))
return given_seq
else:
self.seq += given_seq
self.seq += 'r'
self.seq += self.opposite(given_seq)[::-1]
return self.create_seq(self.seq, num_iterations - 1)
# flip the given sequence.
# If the argument is 'rrl', it will return 'llr'
def opposite(self, str):
seq = ""
for i in str:
if i == 'r':
seq += 'l'
elif i == 'l':
seq += 'r'
return seq
if __name__ == '__main__':
iterations = int(input("Enter number of iterations - "))
length = int(input("Enter length size (recommended 5) - "))
dragon = Dragon_Curve(iterations, length)
turtle.Screen().title("The Dragon Curve")
turtle.done()