首页 > 编程语言 >新手:python里面while循环2——代码优化

新手:python里面while循环2——代码优化

时间:2023-03-02 12:55:57浏览次数:43  
标签:None return python please while layer 代码优化 need last

上一笔记里面,有大量重复的代码,这次来进行优化,如果有其他方法,请教教我,respect!

点击查看代码
# -*- coding:utf-8 -*-
# __author: Andy Liu
# Date: 2023/3/2
menu = {
    "lucky_shop": {
        "fruit": {
            "banana": {"banana need 8yuan": {"This is the last layer, please return": {None}}},
            "apple": {"apple need 7yuan": {"This is the last layer, please return": {None}}},
            "tangerine": {
                "Sugar Tachibana need 5yuan": {"This is the last layer, please return": {None}},
                "Citrus need 8yuan": {"This is the last layer, please return": {None}}

            }
        },
        "stationery": {
            "pencil": {"pencil need 2yuan": {"This is the last layer, please return": {None}}},
            "pen": {"pen need 5yuan": {"This is the last layer, please return": {None}}},
            "notebook": {
                "small need 1yuan": {"This is the last layer, please return": {None}},
                "middle need 2yuan": {"This is the last layer, please return": {None}},
                "big need 3yuan": {"This is the last layer, please return": {None}}
            }
        },
        "kitchen": {
            "knife": {"knife need 10yuna": {"This is the last layer, please return": {None}}},
            "wok": {
                "middle need 20yuan": {"This is the last layer, please return": {None}},
                "big need 25yuan": {"This is the last layer, please return": {None}}
            },
            "spatula": {"spatula need 13yuan": {"This is the last layer, please return": {None}}}
        }
    },
    "seven_eleven": {None},
    "super_market": {None}
}


quit_flag = False
current_layer = menu
previous_layer = []

while not quit_flag:
    print("[press 'b' return to the previous layer.]")
    print("[press 'q' return to exit.]")
    while not quit_flag:
        print("-----------")
        for key in current_layer:
            print(key)
        choice = input(">> ").strip()
        if choice in current_layer:
            previous_layer.append(current_layer)
            current_layer = current_layer[choice]
        elif choice == "b":
            if previous_layer:
                current_layer = previous_layer.pop()
        elif choice == "q":
            quit_flag = True
        else:
            print("invalid input!")

print("Welcome again!")

标签:None,return,python,please,while,layer,代码优化,need,last
From: https://www.cnblogs.com/mrsheep01/p/17171420.html

相关文章

  • python存 文件报错
    withopen("regulation_news_02.json","w")asfile:file.write(json.dumps(data,indent=2,ensure_ascii=False))报错:Traceback(mostrecentcalllast):File......
  • python模块xlsxwriter使用
    1.安装pipinstallXlsxWriter2.使用#-*-coding:utf-8-*-fromioimportBytesIOimportqrcode#[email protected]('/atta......
  • python---文件操作
    1.文件操作步骤打开文件-open读---把文件的内容读到变量里-read 写---把变量的值写到文件内容里-write关闭文件-close2.读取一个文件1)打开文件file=open(要打开......
  • django 源码解读 python manage.py makemigrations
    分析命令之前,需要先了解makemigrations调用的一些类。这样对于后面分析命令时很轻松。1.MigrationRecorder类这个类在django/db/migrations/recorder.py文件中,这个类是......
  • 有趣又实用的python脚本
    1.使用Python进行速度测试这个高级脚本帮助你使用Python测试你的Internet速度。只需安装速度测试模块并运行以下代码。#pipinstallpyspeedtest#pipinstalls......
  • Python抓取数据具体流程
    之前看了一段有关爬虫的网课深有启发,于是自己也尝试着如如何过去爬虫百科“python”词条等相关页面的整个过程记录下来,方便后期其他人一起来学习。抓取策略确定目标:重要......
  • 机器学习python环境搭建
    目的:跑通下面代码相关代码fromtorchimportnnimporttorchimportjiebaimportnumpyasnpraw_text="""越努力就越幸运"""words=list(jieba.cut(raw_text))......
  • Python第三天
    8bit(位)=1byte(字节)1024byte=1kbstr表示字符串(只要是双单引号里的都叫字符串)int表示整数(1、2、3、5)float表示浮点数(3.151) type()数据类型bool表示true,falseint()、str()、fl......
  • python 字符串 格式化输出 槽格式 小数的位数与符号控制
    """槽的格式限定冒号:左边填序号或名称,右边填写格式"""#定义一个数num=3.1465926#保留两位小数,并且四舍五入res="{:.2f}".format(num)print(res)#有符号的数字res2=......
  • python 字符串 格式化输出 槽格式 左中右对齐的内容
    """槽的格式限定冒号:左边填序号或名称,右边填写格式"""#右对齐的字符串,宽度为20s1="{:>20}".format("hello")print(s1)#右对齐,宽20,!填充s2="{:!>20}".format("python")p......