with open('三级菜单', 'r', encoding='utf-8') as f_read:
date = f_read.read()
#print(date.replace(' ', '').replace('\n', ''))
#print(type(date))
menu = eval(date)
current_layer = menu
parent_layer = []
while True:
#打印当前层并进入修改逻辑
print(' '.join(list(current_layer.keys())))
operation = input('进入哪一层(城市名称)修改c 删除d 增加a 返回r 退出q:')
if operation == 'c':
choice_of_city = input('哪个城市:')
if choice_of_city in current_layer:
change_of_city = input('修改成:')
#C++思想,变量存于变量
# s = str(current_layer)
# s = s.replace(choice_of_city, change_of_city)
# current_layer = eval(s)
#py思想,变量存档数据地址,操作变量相当于操作内存
del current_layer[choice_of_city]
current_layer[change_of_city] =
Python入门(七)——文件操作作业:多级菜单高级版,可以修改字典
最新推荐文章于 2024-11-27 21:31:05 发布
本文介绍了Python中实现三级菜单的高级版本,探讨了如何通过字典操作实现文件内容的修改。文章指出,在使用字典时,不同变量实际上指向的是同一存储空间,因此修改字典的一个副本会影响到所有引用该字典的变量,从而实现对多级菜单内容的全局更新。

最低0.47元/天 解锁文章
7181

被折叠的 条评论
为什么被折叠?



