用python设计三个菜单(画多边形,画矩形,画椭圆)
时间: 2023-08-15 09:11:22 浏览: 73
python 超级画板程序源程序.rar
5星 · 资源好评率100%
以下是使用 Python 实现的三个菜单的代码示例,使用了 turtle 库来绘制多边形、矩形和椭圆。
```python
import turtle
# 画多边形
def draw_polygon():
sides = int(input("请输入要画的多边形的边数:"))
length = int(input("请输入多边形的边长:"))
angle = 360 / sides
for i in range(sides):
turtle.forward(length)
turtle.right(angle)
# 画矩形
def draw_rectangle():
width = int(input("请输入矩形的宽度:"))
height = int(input("请输入矩形的高度:"))
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
# 画椭圆
def draw_ellipse():
width = int(input("请输入椭圆的宽度:"))
height = int(input("请输入椭圆的高度:"))
turtle.speed(0)
turtle.penup()
turtle.goto(0, -height)
turtle.pendown()
turtle.color("black")
turtle.fillcolor("gray")
turtle.begin_fill()
turtle.circle(width/2, 90)
turtle.circle(height/2, 90)
turtle.circle(width/2, 90)
turtle.circle(height/2, 90)
turtle.end_fill()
# 主菜单
def main_menu():
print("请选择要绘制的图形:")
print("1. 多边形")
print("2. 矩形")
print("3. 椭圆")
choice = int(input("请输入选项:"))
if choice == 1:
draw_polygon()
elif choice == 2:
draw_rectangle()
elif choice == 3:
draw_ellipse()
else:
print("无效选项,请重新选择。")
main_menu()
# 启动程序
main_menu()
```
注意,上述代码需要在 Python 环境中运行,并且需要安装 turtle 库。在 Windows 系统下,可以通过以下命令安装:
```
pip install turtle
```
阅读全文