生日祝福python编程代码
时间: 2024-06-14 17:02:54 浏览: 27
在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)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)