使用Python制作生日祝福可以通过多种方式实现,包括简单的文本祝福、绘制图形以及发送个性化的邮件等。下面我将为你提供几种不同的方法:
方法一:简单的文本祝福
import datetime
def is_birthday(birthday, year):
birthday_date = datetime.date(year, 2, birthday)
current_date = datetime.date.today()
return current_date.year - birthday_date.year == 1 and current_date.month == birthday_date.month and current_date.day == birthday_date.day
def main():
birthday = int(input("请输入您的生日(格式:YYYYMMDD): "))
year = int(input("请输入当前年份: "))
if is_birthday(birthday, year):
print("恭喜您,今天是您的生日!")
else:
print("今天不是您的生日,请等待!")
if __name__ == "__main__":
main()
方法二:绘制图形祝福
import turtle
import random
def draw_cake(layer, length, color):
if layer == 0:
turtle.color(color)
turtle.begin_fill()
for _ in range(360):
x = draw_x(length, _ * 3)
y = draw_y(length, _ * 3)
turtle.goto(x, y)
turtle.end_fill()
else:
turtle.up()
turtle.goto(0, 0)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
for _ in range(360):
x = draw_x(length, _ * 3)
y = draw_y(length, _ * 3)
turtle.goto(x, y)
turtle.end_fill()
def draw_x(length, angle):
return length * turtle.cos(turtle.radians(angle))
def draw_y(length, angle):
return length * turtle.sin(turtle.radians(angle))
turtle.speed(2)
turtle.penup()
turtle.goto(150, 0)
turtle.pendown()
turtle.pencolor("white")
turtle.write("Happy Birthday", font=("Curlz MT", 50))
turtle.color("blue")
turtle.penup()
turtle.goto(-400, 210)
turtle.pendown()
turtle.write("致:好友友", font=("Curlz MT", 50))
turtle.hideturtle()
turtle.done()
方法三:发送个性化邮件祝福
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_birthday_email(to, name, message):
from_email = ""
password = "your_password"
msg = MIMEMultipart()
msg["From"] = from_email
msg["To"] = to
msg["Subject"] = "生日快乐"
body = f"亲爱的{name},\n\n祝你生日快乐,愿你的每一天都充满快乐和惊喜!\n\n最好的祝福,\n[你的名字]"
msg.attach(MIMEText(body, "plain"))
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login(from_email, password)
server.sendmail(from_email, to, msg.as_string())
server.quit()
示例使用
send_birthday_email("", "John Doe", "亲爱的John,\n\n祝你生日快乐,愿你的每一天都充满快乐和惊喜!\n\n最好的祝福,\n[你的名字]")
以上代码示例展示了如何使用Python进行生日祝福的不同方法。你可以选择最适合你需求的方法进行尝试。