Introduction to Programming using Python 1st Edition

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

Chapter 5 - Loops - Programming Exercises - Page 169: 5.54

Answer

code

Work Step by Step

# 5.54 (Turtle: plot the square function) Write a program that draws a diagram for the # function f(x) = x2 (see Figure 5.6a). import turtle turtle.speed(0) # Fastest # Draw a square function scaleFactor = 0.01 left = -100 right = 100 x = left turtle.penup() turtle.goto(x, scaleFactor * x * x) turtle.pendown() for x in range(left, right + 1): turtle.goto(x, scaleFactor * x * x) # Draw X-axis turtle.penup() turtle.goto(-160, 0) turtle.pendown() turtle.goto(160, 0) # Draw Y-axis turtle.penup() turtle.goto(0, -80) turtle.pendown() turtle.goto(0, 80) # Display X turtle.penup() turtle.goto(0, 80) turtle.pendown() turtle.write("Y") # Display Y turtle.penup() turtle.goto(180, -15) turtle.pendown() turtle.write("X") # Draw arrows turtle.degrees() turtle.penup() turtle.goto(160, 0) turtle.pendown() turtle.setheading(150) turtle.forward(20) turtle.penup() turtle.goto(160, 0) turtle.pendown() turtle.setheading(-150) turtle.forward(20) turtle.penup() turtle.goto(0, 80) turtle.pendown() turtle.setheading(240) turtle.forward(20) turtle.penup() turtle.goto(0, 80) turtle.pendown() turtle.setheading(-60) turtle.forward(20) turtle.hideturtle() 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.