用while循环来写 3*3的正方形,效果如下: * * * * * * * * *
时间: 2023-10-07 18:04:55 浏览: 36
你可以使用两个嵌套的 while 循环来创建一个 3*3 的正方形,如下所示:
```python
row = 1
while row <= 3:
column = 1
while column <= 3:
print("*", end=" ")
column += 1
print()
row += 1
```
输出结果为:
```
* * *
* * *
* * *
```
相关问题
while循环编写程序绘制边长为 100的正方形。
可以使用如下的 Lua 代码来绘制边长为 100 的正方形:
```lua
function love.draw()
local x, y = 100, 100 -- 左上角坐标
local w, h = 100, 100 -- 宽度和高度
love.graphics.rectangle("line", x, y, w, h)
end
```
这个代码使用了 LÖVE 游戏引擎来进行绘制,`love.draw` 函数会在每帧被调用。
我们使用 `love.graphics.rectangle` 函数来绘制一个空心的矩形。第一个参数是绘制模式,这里使用 "line" 表示只绘制边框线。接下来四个参数分别是起点的 x 坐标、y 坐标,以及矩形的宽度和高度。
编写一个完整的程序,要求如下: 创建正方形画布(建议600*600),以画布中心为原点画出坐标轴,按以下公式绘制函数曲线: x = wh×((1+cos(5t)+ sin2(4t))×sin(3t))/5 y = hh×((1+cos(2t)+ sin2(4t))×cos(3t))/5 其中wh、hh的取值分别为画布的半宽和半高,t的取值范围为0至2π,步长为0.01。
这里为您提供一个Python语言编写的程序,可以实现您所要求的功能:
```python
import tkinter as tk
import math
# 画布尺寸
WIDTH = 600
HEIGHT = 600
# 创建画布
root = tk.Tk()
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='white')
canvas.pack()
# 画坐标轴
canvas.create_line(WIDTH/2, 0, WIDTH/2, HEIGHT)
canvas.create_line(0, HEIGHT/2, WIDTH, HEIGHT/2)
# 计算函数曲线
wh = WIDTH / 2
hh = HEIGHT / 2
t = 0
x0 = wh * ((1 + math.cos(5*t) + math.sin(2*4*t)) * math.sin(3*t)) / 5 + wh
y0 = hh * ((1 + math.cos(2*t) + math.sin(2*4*t)) * math.cos(3*t)) / 5 + hh
while t < 2 * math.pi:
t += 0.01
x = wh * ((1 + math.cos(5*t) + math.sin(2*4*t)) * math.sin(3*t)) / 5 + wh
y = hh * ((1 + math.cos(2*t) + math.sin(2*4*t)) * math.cos(3*t)) / 5 + hh
canvas.create_line(x0, y0, x, y)
x0, y0 = x, y
# 运行主循环
root.mainloop()
```
运行程序后,您可以在窗口中看到一个正方形画布,中心为原点,带有坐标轴。程序会按照您所给出的函数公式绘制函数曲线,并在窗口中展示。