编程正方形怎么打出来

时间:2025-01-28 02:19:21 网络游戏

使用Python的turtle库

```python

import turtle

创建绘图窗口

window = turtle.Screen()

window.title("Draw a Square")

window.bgcolor("white")

创建画笔

pen = turtle.Turtle()

pen.color("black")

pen.pensize(3)

绘制正方形的四条边

for _ in range(4):

pen.forward(100) 移动画笔向前

pen.right(90) 调整画笔角度

关闭绘图窗口

window.mainloop()

```

使用Python打印正方形

```python

def draw_square(side_length):

for i in range(side_length):

for j in range(side_length):

print("*", end="")

print()

side_length = int(input("请输入正方形的边长:"))

draw_square(side_length)

```

使用JavaScript在页面上输出正方形

```html