python编写一个简单的日程管理工具包含日程添加,查询日程(按关键字查询),删除日程,显示日程列表(要显示日程序号),编辑日程(按关键字找到原日程并显示再编辑该日程需要修改的地方),退出程序的功能
时间: 2024-05-10 21:18:47 浏览: 98
好的,让我们一步步来完成这个日程管理工具。
首先,我们需要考虑如何存储日程。这里我们可以使用一个字典来存储每个日程,其中键为日程编号,值为一个包含日程信息的字典。
下面是一个例子:
```python
schedules = {
1: {'title': 'Meeting with client', 'date': '2021-05-20', 'time': '10:00', 'location': 'Zoom'},
2: {'title': 'Dentist appointment', 'date': '2021-05-22', 'time': '14:30', 'location': '123 Main St.'},
3: {'title': 'Birthday party', 'date': '2021-05-25', 'time': '18:00', 'location': 'John\'s house'}
}
```
接下来,我们需要定义一些函数来实现日程管理的各种功能。
首先是添加日程的函数。该函数会提示用户输入日程的标题、日期、时间和地点,并自动生成一个日程编号。
```python
def add_schedule():
title = input('Enter title: ')
date = input('Enter date (yyyy-mm-dd): ')
time = input('Enter time (hh:mm): ')
location = input('Enter location: ')
id = max(schedules.keys(), default=0) + 1
schedules[id] = {'title': title, 'date': date, 'time': time, 'location': location}
print('Schedule added successfully.')
```
然后是查询日程的函数。该函数会提示用户输入关键字,并根据关键字查询所有包含该关键字的日程。
```python
def search_schedule():
keyword = input('Enter keyword: ')
found = False
for id, schedule in schedules.items():
if keyword in schedule['title'] or keyword in schedule['location']:
found = True
print(f'{id}: {schedule["title"]} ({schedule["date"]} {schedule["time"]}, {schedule["location"]})')
if not found:
print('No schedules found.')
```
接着是删除日程的函数。该函数会提示用户输入要删除的日程编号,并从字典中删除该日程。
```python
def delete_schedule():
id = int(input('Enter schedule ID: '))
if id in schedules:
del schedules[id]
print('Schedule deleted successfully.')
else:
print('Schedule not found.')
```
然后是显示日程列表的函数。该函数会遍历所有日程,并显示每个日程的编号、标题、日期、时间和地点。
```python
def list_schedules():
for id, schedule in schedules.items():
print(f'{id}: {schedule["title"]} ({schedule["date"]} {schedule["time"]}, {schedule["location"]})')
```
最后是编辑日程的函数。该函数会提示用户输入关键字,查询包含该关键字的日程,并让用户选择要编辑的日程。然后,它会提示用户输入要修改的字段和新值,并更新日程信息。
```python
def edit_schedule():
keyword = input('Enter keyword: ')
found = False
for id, schedule in schedules.items():
if keyword in schedule['title'] or keyword in schedule['location']:
found = True
print(f'{id}: {schedule["title"]} ({schedule["date"]} {schedule["time"]}, {schedule["location"]})')
if not found:
print('No schedules found.')
return
id = int(input('Enter schedule ID to edit: '))
if id not in schedules:
print('Schedule not found.')
return
field = input('Enter field to edit (title, date, time, location): ')
value = input(f'Enter new {field}: ')
schedules[id][field] = value
print('Schedule updated successfully.')
```
最后,我们可以编写一个主函数来实现整个程序的流程。该函数会循环提示用户输入命令,并根据命令调用相应的函数。
```python
def main():
while True:
print('1. Add schedule')
print('2. Search schedule')
print('3. Delete schedule')
print('4. List schedules')
print('5. Edit schedule')
print('6. Quit')
choice = input('Enter choice: ')
if choice == '1':
add_schedule()
elif choice == '2':
search_schedule()
elif choice == '3':
delete_schedule()
elif choice == '4':
list_schedules()
elif choice == '5':
edit_schedule()
elif choice == '6':
break
else:
print('Invalid choice.')
```
现在,我们可以运行主函数来测试我们的日程管理工具了:
```python
if __name__ == '__main__':
main()
```
这个程序比较长,但是我们可以把它们放在一个文件里,这样我们就可以方便地使用它了。
阅读全文