干饭随心选系统
1. 字典模块数据存储字典嵌套是处理 “结构化多维度数据” 的核心方式比如 “饭馆” 作为一个实体包含多个属性用内层字典封装更清晰列表适合存储 “有序的序列数据”如历史记录、菜单字典适合存储 “键值对的映射数据”如饭馆属性。# 干饭随心选系统 print(\n\n 干饭随心选系统 ) # 使用字典存储更详细的饭馆信息 restaurants_dict { 川菜馆: { 类型: 川菜, 电话: 123-4567, 菜单: [麻婆豆腐, 水煮鱼, 宫保鸡丁, 回锅肉], 价格区间: 中等, 评分: 4.5, 营业时间: 11:00-22:00 }, 粤菜馆: { 类型: 粤菜, 电话: 234-5678, 菜单: [白切鸡, 烧鹅, 叉烧, 虾饺], 价格区间: 偏高, 评分: 4.3, 营业时间: 10:30-21:30 }, 湘菜馆: { 类型: 湘菜, 电话: 345-6789, 菜单: [剁椒鱼头, 毛氏红烧肉, 辣椒炒肉], 价格区间: 中等, 评分: 4.2, 营业时间: 11:00-23:00 }, 日料店: { 类型: 日料, 电话: 456-7890, 菜单: [寿司拼盘, 刺身, 拉面, 天妇罗], 价格区间: 偏高, 评分: 4.7, 营业时间: 11:30-22:00 }, 西餐厅: { 类型: 西餐, 电话: 567-8901, 菜单: [牛排, 意大利面, 沙拉, 披萨], 价格区间: 偏高, 评分: 4.4, 营业时间: 11:00-23:00 }, 火锅店: { 类型: 火锅, 电话: 678-9012, 菜单: [麻辣火锅, 清汤火锅, 鸳鸯锅], 价格区间: 中等, 评分: 4.6, 营业时间: 10:00-24:00 } } # 历史选择记录 lunch_history [] dinner_history []1字典设计逻辑渐进初级阶段如果是新手入门可能先只用 “单层字典” 存储基础信息比如 restaurants {川菜馆: 123-4567, 粤菜馆: 234-5678}只能存 “名称 - 电话”无法承载多维度信息。进阶阶段采用 “嵌套字典 列表” 的结构化设计外层字典key饭馆名称value该饭馆的所有信息内层字典实现 “按名称快速查找”内层字典key信息维度类型/电话/菜单等value对应值其中 “菜单” 用列表存储多菜品适配 “多个值” 的场景历史记录用列表 lunch_history [ ] 、dinner_history [ ] 存储利用列表 “有序、可重复、支持 append/remove” 的特性记录选择顺序。2. 函数功能模块业务逻辑函数模块整体设计思路所有函数遵循 “单一职责原则”—— 一个函数只做一件事。功能点增删改查查询饭馆信息def show_all_restaurants(): 显示所有饭馆详细信息 print(\n所有饭馆详细信息:) for name, info in restaurants_dict.items(): print(f\n{name}:) print(f 类型: {info[类型]}) print(f 电话: {info[电话]}) print(f 菜单: {, .join(info[菜单])}) print(f 价格: {info[价格区间]}) print(f 评分: {info[评分]}) print(f 营业时间: {info[营业时间]})修改饭馆详情def modify_restaurant(): 修改饭馆信息 print(\n修改饭馆信息:) # 先显示所有饭馆供用户选择 if not restaurants_dict: print(当前系统中没有饭馆信息) return print(当前系统中的饭馆:) for i, name in enumerate(restaurants_dict.keys(), 1): print(f{i}. {name}) restaurant_name input(\n请输入要修改的饭馆名称: ) if restaurant_name in restaurants_dict: print(f\n当前 {restaurant_name} 的信息:) info restaurants_dict[restaurant_name] print(f类型: {info[类型]}) print(f电话: {info[电话]}) print(f菜单: {, .join(info[菜单])}) print(f价格区间: {info[价格区间]}) print(f评分: {info[评分]}) print(f营业时间: {info[营业时间]}) print(\n请输入新的信息直接回车保持原值:) new_type input(f类型 [{info[类型]}]: ) or info[类型] new_phone input(f电话 [{info[电话]}]: ) or info[电话] new_menu_input input(f菜单 [{, .join(info[菜单])}]: ) new_menu new_menu_input.split(,) if new_menu_input else info[菜单] new_price input(f价格区间 [{info[价格区间]}]: ) or info[价格区间] new_score_input input(f评分 [{info[评分]}]: ) new_score float(new_score_input) if new_score_input else info[评分] new_hours input(f营业时间 [{info[营业时间]}]: ) or info[营业时间] # 更新信息 restaurants_dict[restaurant_name] { 类型: new_type, 电话: new_phone, 菜单: new_menu, 价格区间: new_price, 评分: new_score, 营业时间: new_hours } print(f成功更新 {restaurant_name} 的信息) else: print(f未找到名为 {restaurant_name} 的饭馆)删除现有饭馆def delete_restaurant(): 删除饭馆 print(\n删除饭馆:) # 先显示所有饭馆供用户选择 if not restaurants_dict: print(当前系统中没有饭馆信息) return print(\n删除方式:) print(1. 按名称删除单个饭馆) print(2. 按条件批量删除) delete_choice input(请选择删除方式1-2: ) if delete_choice 1: # 单个删除逻辑 print(\n当前系统中的饭馆:) for i, name in enumerate(restaurants_dict.keys(), 1): print(f{i}. {name}) restaurant_name input(\n请输入要删除的饭馆名称: ) if restaurant_name in restaurants_dict: # 确认删除 confirm input(f确定要删除 {restaurant_name} 吗(y/n): ) if confirm.lower() y: del restaurants_dict[restaurant_name] print(f成功删除 {restaurant_name}) # 同时从历史记录中删除 if restaurant_name in lunch_history: lunch_history.remove(restaurant_name) if restaurant_name in dinner_history: dinner_history.remove(restaurant_name) else: print(取消删除操作) else: print(f未找到名为 {restaurant_name} 的饭馆) elif delete_choice 2: # 批量删除逻辑这里需要倒序遍历 print(\n批量删除条件:) print(1. 按类型删除) print(2. 按价格区间删除) print(3. 按评分删除) batch_choice input(请选择批量删除条件1-3: ) restaurants_to_delete [] if batch_choice 1: type_filter input(请输入要删除的类型川菜/粤菜/湘菜/日料/西餐/火锅: ) restaurants_to_delete [name for name, info in restaurants_dict.items() if info[类型] type_filter] elif batch_choice 2: price_filter input(请输入要删除的价格区间中等/偏高: ) restaurants_to_delete [name for name, info in restaurants_dict.items() if info[价格区间] price_filter] elif batch_choice 3: max_score float(input(请输入最高评分删除评分低于此值的饭馆: )) restaurants_to_delete [name for name, info in restaurants_dict.items() if info[评分] max_score] else: print(无效选择) return if not restaurants_to_delete: print(没有找到符合条件的饭馆) return print(f\n找到 {len(restaurants_to_delete)} 家符合条件的饭馆:) for i, name in enumerate(restaurants_to_delete, 1): print(f{i}. {name}) confirm input(f\n确定要删除这 {len(restaurants_to_delete)} 家饭馆吗(y/n): ) if confirm.lower() y: # 这里需要倒序遍历列表来安全删除 deleted_count 0 for i in range(len(restaurants_to_delete)-1, -1, -1): name restaurants_to_delete[i] del restaurants_dict[name] # 同时从历史记录中删除 if name in lunch_history: lunch_history.remove(name) if name in dinner_history: dinner_history.remove(name) deleted_count 1 print(f已删除: {name}) print(f\n成功批量删除 {deleted_count} 家饭馆) else: print(取消批量删除操作) else: print(无效选择)添加新饭馆信息def add_new_restaurant(): 添加新饭馆 print(\n添加新饭馆:) name input(饭馆名称: ) type_ input(饭馆类型: ) phone input(联系电话: ) menu input(菜单用逗号分隔: ).split(,) price input(价格区间中等/偏高: ) score float(input(评分0-5: )) hours input(营业时间: ) restaurants_dict[name] { 类型: type_, 电话: phone, 菜单: menu, 价格区间: price, 评分: score, 营业时间: hours } print(f成功添加 {name} 到系统)智能推荐防重复机制避免连续推荐同一饭馆根据时间午餐/晚餐自动推荐饭馆def smart_recommendation(meal_type, history): 智能推荐饭馆避免重复选择 import random # 获取所有饭馆名称 all_restaurants list(restaurants_dict.keys()) # 如果历史记录为空随机选择 if not history: return random.choice(all_restaurants) # 避免最近选择的饭馆 recent_choices history[-3:] # 最近3次选择 available_choices [r for r in all_restaurants if r not in recent_choices] # 如果所有饭馆都在最近选择过则从所有中选择 if not available_choices: available_choices all_restaurants return random.choice(available_choices) def select_meal(meal_type, history): 智能选择餐食午餐/晚餐 selected_name smart_recommendation(meal_type, history) selected_info restaurants_dict[selected_name] history.append(selected_name) print(f\n{meal_type}智能推荐: {selected_name}) print(f类型: {selected_info[类型]}) print(f电话: {selected_info[电话]}) print(f推荐菜单: {, .join(selected_info[菜单])}) print(f价格区间: {selected_info[价格区间]}) print(f评分: {selected_info[评分]}) print(f营业时间: {selected_info[营业时间]})按条件筛选支持按菜系、价格、评分等条件筛选def filter_restaurants(): 按条件筛选饭馆 print(\n筛选条件:) print(1. 按类型筛选) print(2. 按价格筛选) print(3. 按评分筛选) filter_choice input(请选择筛选方式1-3: ) if filter_choice 1: type_filter input(请输入类型川菜/粤菜/湘菜/日料/西餐/火锅: ) filtered {name: info for name, info in restaurants_dict.items() if info[类型] type_filter} elif filter_choice 2: price_filter input(请输入价格区间中等/偏高: ) filtered {name: info for name, info in restaurants_dict.items() if info[价格区间] price_filter} elif filter_choice 3: min_score float(input(请输入最低评分0-5: )) filtered {name: info for name, info in restaurants_dict.items() if info[评分] min_score} else: print(无效选择) return if filtered: print(f\n找到 {len(filtered)} 家符合条件的饭馆:) for name, info in filtered.items(): print(f{name}: {info[类型]}, 评分{info[评分]}, 价格{info[价格区间]}) else: print(未找到符合条件的饭馆)查看历史记录提供历史数据查询功能记录用户访问和推荐历史def show_selection_history(): 显示选择历史 print(\n午餐选择历史:) for i, restaurant in enumerate(lunch_history, 1): print(f{i}. {restaurant}) print(\n晚餐选择历史:) for i, restaurant in enumerate(dinner_history, 1): print(f{i}. {restaurant})3. 主程序循环模块交互入口用户交互界面核心。菜单式循环交互主菜单选项查看饭馆信息智能推荐午餐/晚餐按条件筛选查看历史记录管理饭馆添加、删除、修改退出系统循环逻辑用户选择后返回菜单直至退出# 主程序循环 while True: print(\n *60) print(欢迎使用干饭随心选系统) print(1. 查看所有饭馆详细信息) print(2. 智能选择午餐避免重复) print(3. 智能选择晚餐避免重复) print(4. 按条件筛选) print(5. 查看选择历史) print(6. 添加新饭馆) print(7. 删除饭馆) print(8. 修改饭馆信息) print(9. 退出系统) choice input(请选择功能1-9: ) if choice 1: show_all_restaurants() elif choice 2: select_meal(午餐, lunch_history) elif choice 3: select_meal(晚餐, dinner_history) elif choice 4: filter_restaurants() elif choice 5: show_selection_history() elif choice 6: add_new_restaurant() elif choice 7: delete_restaurant() elif choice 8: modify_restaurant() elif choice 9: print(感谢使用干饭随心选系统再见) break else: print(无效选择请重新输入1-9)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2424110.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!