Import turtle
board = turtle.Turtle()
board.speed(0)
board.color("red")
board.up()
board.goto(-150, 100)
board.down()
board.pensize(5)
for i in range(2):
board.forward(300)
board.right(90)
board.forward(200)
board.right(90)
board.hideturtle()
ball = turtle.Turtle()
ball.shape('circle')
x = 0
dx = 2
while True:
if x > 150 or x < -150:
dx = -dx
x = x + dx
ball.goto(x, 0) Допоможіть найти помилку щоб намалювати мяч в програмі ПАйтон
Answers & Comments
Ответ:
The error in the code is the indentation in the for loop. The lines after the for loop should be indented to be inside the loop. Here's the corrected code:
```python
import turtle
board = turtle.Turtle()
board.speed(0)
board.color("red")
board.up()
board.goto(-150, 100)
board.down()
board.pensize(5)
for i in range(2):
board.forward(300)
board.right(90)
board.forward(200)
board.right(90)
board.hideturtle()
ball = turtle.Turtle()
ball.shape('circle')
x = 0
dx = 2
while True:
if x > 150 or x < -150:
dx = -dx
x = x + dx
ball.goto(x, 0)
```
This should draw a red rectangle and move a circle-shaped ball back and forth horizontally.