计算机的指令用Python编写程序,绘制图形: 编程要求 请编写程序,当输入1时,显示左图(心型),输入2时,显示右图(笑脸)。
时间: 2024-04-30 09:20:02 浏览: 51
可以使用Python的turtle库来绘制图形,下面是实现上述要求的代码:
```python
import turtle
def draw_heart():
turtle.color('red')
turtle.begin_fill()
turtle.left(140)
turtle.forward(180)
turtle.circle(-90, 200)
turtle.setheading(60)
turtle.circle(-90, 200)
turtle.forward(180)
turtle.end_fill()
def draw_smiley():
turtle.color('yellow')
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
turtle.penup()
turtle.goto(-40, 120)
turtle.pendown()
turtle.color('black')
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(40, 120)
turtle.pendown()
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(0, 80)
turtle.pendown()
turtle.width(10)
turtle.right(90)
turtle.circle(50, 180)
while True:
num = int(input('请输入数字(1或2):'))
if num == 1:
draw_heart()
elif num == 2:
draw_smiley()
else:
print('输入错误,请重新输入!')
```
运行程序后,根据要求输入1或2即可看到相应的图形。
阅读全文