python 日程管理程序具有添加日程、查询日程、删除日程、显示日程列表、编辑日程和退出程序的功能
时间: 2023-10-01 16:04:54 浏览: 52
好的,这个任务可以通过使用 Python 编写一个简单的控制台应用程序来实现。以下是一个基本的框架,你可以在此基础上进行修改和完善。
```python
import datetime
# 存储所有日程的列表
schedules = []
# 添加日程
def add_schedule():
date_str = input("请输入日期(格式为YYYY-MM-DD):")
time_str = input("请输入时间(格式为HH:MM):")
description = input("请输入日程内容:")
try:
date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
time = datetime.datetime.strptime(time_str, '%H:%M')
schedule = {'date': date, 'time': time, 'description': description}
schedules.append(schedule)
print("日程添加成功!")
except ValueError:
print("日期或时间格式不正确,请重新输入。")
# 查询日程
def search_schedule():
date_str = input("请输入要查询的日期(格式为YYYY-MM-DD):")
try:
date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
result = []
for schedule in schedules:
if schedule['date'] == date:
result.append(schedule)
if len(result) == 0:
print("该日期无日程安排。")
else:
print("查询结果如下:")
for schedule in result:
print(schedule['time'].strftime('%H:%M'), schedule['description'])
except ValueError:
print("日期格式不正确,请重新输入。")
# 删除日程
def delete_schedule():
date_str = input("请输入要删除的日期(格式为YYYY-MM-DD):")
time_str = input("请输入要删除的时间(格式为HH:MM):")
try:
date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
time = datetime.datetime.strptime(time_str, '%H:%M')
for schedule in schedules:
if schedule['date'] == date and schedule['time'] == time:
schedules.remove(schedule)
print("日程删除成功!")
break
else:
print("指定的日程不存在。")
except ValueError:
print("日期或时间格式不正确,请重新输入。")
# 显示日程列表
def show_schedules():
if len(schedules) == 0:
print("暂无日程安排。")
else:
print("当前所有日程如下:")
for schedule in schedules:
print(schedule['date'].strftime('%Y-%m-%d'), schedule['time'].strftime('%H:%M'), schedule['description'])
# 编辑日程
def edit_schedule():
date_str = input("请输入要编辑的日期(格式为YYYY-MM-DD):")
time_str = input("请输入要编辑的时间(格式为HH:MM):")
try:
date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
time = datetime.datetime.strptime(time_str, '%H:%M')
for schedule in schedules:
if schedule['date'] == date and schedule['time'] == time:
new_date_str = input("请输入新的日期(格式为YYYY-MM-DD),不修改请直接回车:")
new_time_str = input("请输入新的时间(格式为HH:MM),不修改请直接回车:")
new_description = input("请输入新的日程内容,不修改请直接回车:")
if new_date_str:
new_date = datetime.datetime.strptime(new_date_str, '%Y-%m-%d')
schedule['date'] = new_date
if new_time_str:
new_time = datetime.datetime.strptime(new_time_str, '%H:%M')
schedule['time'] = new_time
if new_description:
schedule['description'] = new_description
print("日程编辑成功!")
break
else:
print("指定的日程不存在。")
except ValueError:
print("日期或时间格式不正确,请重新输入。")
# 主程序
while True:
print("请选择操作:")
print("1. 添加日程\n2. 查询日程\n3. 删除日程\n4. 显示日程列表\n5. 编辑日程\n6. 退出程序")
choice = input()
if choice == '1':
add_schedule()
elif choice == '2':
search_schedule()
elif choice == '3':
delete_schedule()
elif choice == '4':
show_schedules()
elif choice == '5':
edit_schedule()
elif choice == '6':
print("程序已退出。")
break
else:
print("输入有误,请重新输入。")
```
以上代码仅提供了一个基本框架,你可以根据自己的需求对其进行修改和完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)