建立三个菜单,画多边形,画矩形,画椭圆
时间: 2023-09-24 20:05:40 浏览: 120
以下是一个简单的Python代码示例,实现了三个菜单:画多边形、画矩形、画椭圆。你可以在此基础上进行修改和扩展。
```python
import turtle
def draw_polygon():
sides = int(input("请输入多边形的边数: "))
length = int(input("请输入多边形的边长: "))
angle = 360 / sides
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
for i in range(sides):
turtle.forward(length)
turtle.right(angle)
turtle.done()
def draw_rectangle():
width = int(input("请输入矩形的宽度: "))
height = int(input("请输入矩形的高度: "))
turtle.penup()
turtle.goto(-width / 2, -height / 2)
turtle.pendown()
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.done()
def draw_ellipse():
width = int(input("请输入椭圆的宽度: "))
height = int(input("请输入椭圆的高度: "))
turtle.penup()
turtle.goto(0, -height / 2)
turtle.pendown()
turtle.circle(width / 2, 90)
turtle.circle(height / 2, 90)
turtle.circle(width / 2, 90)
turtle.circle(height / 2, 90)
turtle.done()
while True:
print("请选择要绘制的图形:")
print("1. 多边形")
print("2. 矩形")
print("3. 椭圆")
print("0. 退出")
choice = int(input("请输入选项: "))
if choice == 1:
draw_polygon()
elif choice == 2:
draw_rectangle()
elif choice == 3:
draw_ellipse()
elif choice == 0:
break
else:
print("无效的选项,请重新输入。")
```
阅读全文