Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 15 - Recursion - Programming Exercises - Page 528: 15.34

Answer

code

Work Step by Step

# 15.34 (Turtle: Hilbert curve) Rewrite the Hilbert curve in Exercise 15.33 using Turtle, # as shown in Figure 15.20. Your program should prompt the user to enter the # order and display the corresponding fractal for the order. import turtle def upperU(order): if order > 0: leftU(order - 1) turtle.setheading(270) turtle.forward(length) upperU(order - 1) turtle.setheading(0) turtle.forward(length) upperU(order - 1) turtle.setheading(90) turtle.forward(length) rightU(order - 1) def leftU(order): if order > 0: upperU(order - 1) turtle.setheading(0) turtle.forward(length) leftU(order - 1) turtle.setheading(270) turtle.forward(length) leftU(order - 1) turtle.setheading(180) turtle.forward(length) downU(order - 1) def rightU(order): if order > 0: downU(order - 1) turtle.setheading(180) turtle.forward(length) rightU(order - 1) turtle.setheading(90) turtle.forward(length) rightU(order - 1) turtle.setheading(0) turtle.forward(length) upperU(order - 1) def downU(order): if order > 0: rightU(order - 1) turtle.setheading(90) turtle.forward(length) downU(order - 1) turtle.setheading(180) turtle.forward(length) downU(order - 1) turtle.setheading(270) turtle.forward(length) leftU(order - 1) order = eval(input("Enter an order: ")) SIZE = 400 length = 400 for i in range(order): length = length / 2 # Get the right length for the order # Get the start point x = -SIZE / 2 + length / 2 y = SIZE / 2 - length / 2 turtle.penup() turtle.goto(x, y) turtle.pendown() upperU(order) turtle.done()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.