python生日快乐代码高级
时间: 2024-09-17 18:08:53 浏览: 371
在Python中,编写一个高级的“生日快乐”代码通常会涉及一些交互式、动态和有趣的功能。例如,你可以创建一个简单的命令行程序,利用字典存储不同人的姓名和生日,然后通过用户输入来查询并打印祝福信息。下面是一个示例:
```python
class BirthdayBook:
def __init__(self):
self.birthday_dict = {}
def add_birthday(self, name, birth_date):
self.birthday_dict[name] = birth_date
def check_birthday(self, name):
today = datetime.date.today()
if today.month == int(birth_date.split('/')[0]) and today.day == int(birth_date.split('/')[1]):
print(f"Happy birthday, {name}! 🎈")
else:
print(f"{name}, no birthday today.")
# 使用示例
birthday_book = BirthdayBook()
birthday_book.add_birthday("Alice", "01/01")
birthday_book.add_birthday("Bob", "12/25")
name = input("Enter someone's name to wish 'Happy Birthday': ")
if name in birthday_book.birthday_dict:
birthday_book.check_birthday(name)
else:
print("No birthday record found for that person.")
阅读全文