生日祝福python编程代码
时间: 2024-06-14 09:02:54 浏览: 215
在Python中,你可以编写一个简单的程序来生成个性化的生日祝福。以下是一个例子,使用了字符串格式化和随机模块来增加趣味性:
```python
import random
# 定义一个包含祝福语的列表
greetings = ["Happy birthday!", "Wishing you a fantastic day!",
"May your special day be filled with joy!",
"A year older, a million reasons to smile.",
"Blow out the candles and make a wish!"]
# 获取用户的生日信息(假设今天是他们的生日)
def get_birthday():
return input("Please enter your birthday (DD/MM/YYYY): ")
# 生成随机的祝福语
def generate_wish(birthday):
today = datetime.date.today()
birthdate = datetime.datetime.strptime(birthday, "%d/%m/%Y").date()
age = today.year - birthdate.year
if today.day == birthdate.day:
return random.choice(greetings) + f", dear one. You are {age} today!"
else:
return random.choice(greetings) + f"Even though it's not your birthday yet, I still wanted to say Happy Early Birthday!"
# 主程序
birthday = get_birthday()
wish = generate_wish(birthday)
print(wish)
```
阅读全文