生日蛋糕python
时间: 2023-10-12 12:06:19 浏览: 47
以下是一个简单的 Python 生日蛋糕程序:
```python
age = input("请输入你的年龄:")
name = input("请输入你的名字:")
print("生日快乐," + name + "!")
print("你已经 " + age + " 岁了!")
# 生日蛋糕图案
cake = """
_____ _____
/ \ / \\
| | |
\ / \ /
\___/ \___/
"""
print(cake)
```
运行程序后,会提示你输入你的年龄和名字,然后输出生日快乐的祝福语和一个生日蛋糕图案。
相关问题
生日蛋糕Python
以下是使用Python中的turtle库绘制生日蛋糕的代码和效果图:
```python
import turtle
# 绘制圆形
def draw_circle(radius, fill_color):
turtle.fillcolor(fill_color)
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
# 绘制矩形
def draw_rectangle(width, height, fill_color):
turtle.fillcolor(fill_color)
turtle.begin_fill()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.end_fill()
# 绘制生日蛋糕
def draw_cake(width, height, colors_a, h):
# 绘制蛋糕底面
draw_circle(width/2, colors_a[0])
# 绘制蛋糕身体
turtle.penup()
turtle.goto(0, h)
turtle.pendown()
draw_circle(width/2, colors_a[1])
# 绘制蛋糕上面的矩形
turtle.penup()
turtle.goto(-width/2, h+height)
turtle.pendown()
draw_rectangle(width, height, colors_a[2])
# 设置画笔和画布
turtle.setup(800, 600)
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.pensize(5)
turtle.speed(10)
# 绘制生日蛋糕
draw_cake(200, 50, ['pink', 'purple', 'white'], 0)
# 隐藏画笔
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
效果图如下:
![birthday_cake](https://i.imgur.com/5JZJzvL.png)
生日蛋糕Python代码
生日蛋糕Python代码可以使用turtle库绘制。以下是一个简单的示例代码:
```python
import turtle
# 绘制一个圆形
def draw_circle(radius):
turtle.color("pink")
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
# 绘制一个矩形
def draw_rect(width, height):
turtle.color("brown")
turtle.begin_fill()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.end_fill()
# 绘制蛋糕
def draw_cake(radius, height):
draw_circle(radius)
turtle.penup()
turtle.goto(0, height)
turtle.pendown()
draw_rect(radius * 2, height)
# 绘制蜡烛
def draw_candle(x, y, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color("yellow")
turtle.begin_fill()
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.end_fill()
# 绘制生日快乐字样
def draw_text():
turtle.penup()
turtle.goto(-150, 150)
turtle.pendown()
turtle.color("red")
turtle.write("Happy Birthday!", font=("Arial", 24, "normal"))
# 主函数,绘制整个蛋糕
def main():
turtle.setup(800, 600)
draw_cake(100, 50)
draw_candle(-40, 100, 30)
draw_candle(20, 120, 40)
draw_text()
turtle.done()
if __name__ == '__main__':
main()
```
以上代码通过使用turtle库绘制了一个生日蛋糕,并且还绘制了两根蜡烛和生日快乐字样。你可以根据自己的需要进行修改和调整。