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 167: 5.47

Answer

code

Work Step by Step

# 5.47 (Turtle: draw random balls) Write a program that displays 10 random balls in # a rectangle with width 120 and height 100, centered at (0, 0), as shown in # Figure 5.3a. import random import turtle WIDTH = 120 HEIGHT = 100 count = 0 turtle.penup() turtle.goto(0 - WIDTH / 2, 0 - HEIGHT / 2) turtle.pendown() for i in range(4): if i % 2 == 0: turtle.forward(WIDTH) else: turtle.forward(HEIGHT) turtle.left(90) while count < 10: turtle.penup() x = random.randint(-WIDTH / 2, WIDTH / 2) y = random.randint(-HEIGHT / 2, HEIGHT / 2) turtle.goto(x, y) turtle.pendown() turtle.dot(5, "red") count += 1 turtle.ht() # Same as 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.