用python的字典编写行程表的程序设计
时间: 2024-01-26 14:42:05 浏览: 24
好的,以下是用 Python 字典编写行程表的程序设计示例:
```
schedule = {}
while True:
print("1. 添加行程")
print("2. 删除行程")
print("3. 查看行程")
print("4. 退出程序")
choice = input("请选择操作:")
if choice == "1":
date = input("请输入日期(格式:YYYY-MM-DD):")
event = input("请输入行程:")
if date in schedule:
schedule[date].append(event)
else:
schedule[date] = [event]
print("添加成功!")
elif choice == "2":
date = input("请输入要删除行程的日期(格式:YYYY-MM-DD):")
if date in schedule:
events = schedule[date]
for i, event in enumerate(events):
print(f"{i+1}. {event}")
choice = input("请选择要删除的行程(输入行程前的数字):")
try:
index = int(choice) - 1
del events[index]
print("删除成功!")
except:
print("输入有误,删除失败!")
else:
print("该日期没有行程!")
elif choice == "3":
date = input("请输入要查看行程的日期(格式:YYYY-MM-DD):")
if date in schedule:
events = schedule[date]
for i, event in enumerate(events):
print(f"{i+1}. {event}")
else:
print("该日期没有行程!")
elif choice == "4":
print("程序已退出。")
break
else:
print("输入有误,请重新输入!")
```
该程序通过一个 `schedule` 字典来存储行程,其中日期作为键,对应的行程列表作为值。程序提供了添加行程、删除行程、查看行程以及退出程序的功能,用户可以根据提示进行操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![](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)