• python_Day08


    今天完善了昨晚的三级目录的程序,并进一步的学习了文件的读写三种模式,其笔记如下所示:

    ******三级目录程序复杂版笔记******

    #Author:"haijing"
    #date:2018/7/6

    #三级菜单 要求:
    #1、可以一层一层的进入到所有层
    #2、可以在每层返回上一层
    #3、可以在任意层退出 主菜单

    # menu={
    # '北京':{},
    # '上海':{},
    # '山东':{},
    # }

    # menu={
    # '北京':{
    # '朝阳':{},
    # '昌平':{},
    # '海淀':{}
    # },
    # '上海':{},
    # '山东':{},
    # }

    # menu={
    # '北京':{
    # '朝阳':{
    # '国贸': {},
    # '望京': {},
    # '三里屯':{},
    # },
    # '昌平':{},
    # '海淀':{}
    # },
    # '上海':{},
    # '山东':{},
    # }

    menu={
    '北京':{
    '朝阳':{
    '国贸': {
    'CICC':{},
    'HP':{},
    '渣打银行':{},
    'CCTV':{},
    },
    '望京': {
    '陌陌':{},
    '奔驰':{},
    '360':{},
    },
    '三里屯':{
    '优衣库':{},
    'apple':{},
    },
    },
    '昌平':{
    '沙河':{
    '老男孩':{},
    '阿泰包子':{},
    },
    '天通苑':{
    '链家':{},
    '我爱我家':{},
    },
    '回龙观':{},
    },
    '海淀':{
    '五道口':{
    '谷歌':{},
    '网易':{},
    '搜狐':{},
    '搜狗':{},
    '快手':{},
    },
    '中关村':{
    'youku':{},
    'Iqiyi':{},
    '汽车之家':{},
    '新东方':{},
    'QQ':{},
    },
    }
    },
    '上海':{
    '浦东':{
    '陆家嘴':{
    'CICC':{},
    '高盛':{},
    '摩根':{},
    },
    '外滩':{},
    },
    '闵行':{},
    '静安':{},
    },
    '山东':{
    '济南':{},
    '德州':{
    '乐陵':{
    '丁乌镇':{},
    '城区':{},
    },
    '平原':{},
    },
    '青岛':{},
    },
    }
    # while 1:  #第一个while
    # for key in menu:
    # print(key) #打印一级目录 相当于北京
    # choice1=input("1>>").strip() #strip的作用是去重,这里不加也可以
    # if choice1 in menu:
    # while 1: #第二个while,防止执行完119行后,再返回到第一个while中执行
    # for key2 in menu[choice1]:
    # print(key2) #打印二级目录 相当于昌平
    # choice2 = input("2>>").strip()
    # if choice2 in menu[choice1]:
    # while 1: #第三个while,防止执行完124行后,再返回到第二个while中执行
    # for key3 in menu[choice1][choice2]:
    # print(key3) #打印三级目录 相当于沙河
    # choice3 = input("3>>").strip()
    # if choice3 in menu[choice1][choice2]: #刚刚choice3和if没有对齐,导致出错
    # while 1:
    # for key4 in menu[choice1][choice2][choice3]:
    # print(key4) #打印四级目录 相当于老男孩
    # choice4=input("4>>").strip()
    # print('last level')

    # #以上只是能进入这四层目录,接下来再改进,解决回退的问题,做一个标志位,改变while循环的条件
    # back_flag=0 #做一个全局的标志位
    # while not back_flag: #第一个while
    # for key in menu:
    # print(key) #打印一级目录 相当于北京
    # choice1=input("1>>").strip() #strip的作用是去重,这里不加也可以
    # # if choice1 == 'b': # 注意这里有两个等号,刚才写了一个 退回上一级标志位 第一层不能加,要不然就退出程序了
    # # back_flag = 1 # 执行完这一句后,第四个while中条件不成立,执行else
    # if choice1 in menu:
    # while not back_flag: #第二个while,防止执行完119行后,再返回到第一个while中执行
    # for key2 in menu[choice1]:
    # print(key2) #打印二级目录 相当于昌平
    # choice2 = input("2>>").strip()
    # if choice2 == 'b': # 注意这里有两个等号,刚才写了一个 退回上一级标志位
    # back_flag = 1 # 执行完这一句后,第四个while中条件不成立,执行else
    # if choice2 in menu[choice1]:
    # while not back_flag: #第三个while,防止执行完124行后,再返回到第二个while中执行
    # for key3 in menu[choice1][choice2]:
    # print(key3) #打印三级目录 相当于沙河
    # choice3 = input("3>>").strip()
    # if choice3 == 'b': # 注意这里有两个等号,刚才写了一个 退回上一级标志位
    # back_flag = 1 # 执行完这一句后,第四个while中条件不成立,执行else
    # if choice3 in menu[choice1][choice2]: #刚刚choice3和if没有对齐,导致出错
    # while not back_flag: #第四个while,
    # for key4 in menu[choice1][choice2][choice3]:
    # print(key4) #打印四级目录 相当于老男孩
    # choice4=input("4>>").strip()
    # print('last level')
    # if choice4=='b': #注意这里有两个等号,刚才写了一个 退回上一级标志位
    # back_flag=1 #执行完这一句后,第四个while中条件不成立,执行else
    # else:
    # back_flag=0 #执行完这一句后,返回到第三个while中继续执行,判断条件是否成立 #在上一级while循环中,继续循环执行程序的保证
    # else:
    # back_flag=0 #在上一级while循环中,继续循环执行程序的保证
    # else:
    # back_flag=0 #在上一级while循环中,继续循环执行程序的保证


    #以上只是能进入这四层目录,接下来再改进,解决回退的问题,做一个标志位,改变while循环的条件
    #加退出 输入q退出
    back_flag=0 #做一个返回上一层全局的标志位
    exit_flag=0 #做一个退出程序的标志位
    while not back_flag and not exit_flag: #第一个while
    for key in menu:
    print(key) #打印一级目录 相当于北京
    choice1=input("1>>").strip() #strip的作用是去重,这里不加也可以
    # if choice1 == 'b': # 注意这里有两个等号,刚才写了一个 退回上一级标志位 第一层不能加,要不然就退出程序了
    # back_flag = 1 # 执行完这一句后,第四个while中条件不成立,执行else
    if choice1 == 'q': # 在第一层输入q退出
    exit_flag = 1
    if choice1 in menu:
    while not back_flag and not exit_flag: #第二个while,防止执行完119行后,再返回到第一个while中执行
    for key2 in menu[choice1]:
    print(key2) #打印二级目录 相当于昌平
    choice2 = input("2>>").strip()
    if choice2 == 'b': # 注意这里有两个等号,刚才写了一个 退回上一级标志位
    back_flag = 1 # 执行完这一句后,第四个while中条件不成立,执行else
    if choice2 == 'q': # 在第二层输入q退出
    exit_flag = 1
    if choice2 in menu[choice1]:
    while not back_flag and not exit_flag: #第三个while,防止执行完124行后,再返回到第二个while中执行
    for key3 in menu[choice1][choice2]:
    print(key3) #打印三级目录 相当于沙河
    choice3 = input("3>>").strip()
    if choice3 == 'b': # 注意这里有两个等号,刚才写了一个 退回上一级标志位
    back_flag = 1 # 执行完这一句后,第四个while中条件不成立,执行else
    if choice3 == 'q': # 在第三层输入q退出
    exit_flag = 1
    if choice3 in menu[choice1][choice2]: #刚刚choice3和if没有对齐,导致出错
    while not back_flag and not exit_flag: #第四个while,
    for key4 in menu[choice1][choice2][choice3]:
    print(key4) #打印四级目录 相当于老男孩
    choice4=input("4>>").strip()
    print('last level')
    if choice4=='b': #注意这里有两个等号,刚才写了一个 退回上一级标志位
    back_flag=1 #执行完这一句后,第四个while中条件不成立,执行else
    if choice4=='q': #在第四层输入q退出
    exit_flag=1
    else:
    back_flag=0 #执行完这一句后,返回到第三个while中继续执行,判断条件是否成立 #在上一级while循环中,继续循环执行程序的保证
    else:
    back_flag=0 #在上一级while循环中,继续循环执行程序的保证
    else:
    back_flag=0 #在上一级while循环中,继续循环执行程序的保证

    ******三级目录程序简洁版笔记******
    #Author:"haijing"
    #date:2018/7/7

    #此时能实现四级的循环
    # menu={
    # '北京':{
    # '朝阳':{
    # '国贸': {
    # 'CICC':{},
    # 'HP':{},
    # '渣打银行':{},
    # 'CCTV':{},
    # },
    # '望京': {
    # '陌陌':{},
    # '奔驰':{},
    # '360':{},
    # },
    # '三里屯':{
    # '优衣库':{},
    # 'apple':{},
    # },
    # },
    # '昌平':{
    # '沙河':{
    # '老男孩':{},
    # '阿泰包子':{},
    # },
    # '天通苑':{
    # '链家':{},
    # '我爱我家':{},
    # },
    # '回龙观':{},
    # },
    # '海淀':{
    # '五道口':{
    # '谷歌':{},
    # '网易':{},
    # '搜狐':{},
    # '搜狗':{},
    # '快手':{},
    # },
    # '中关村':{
    # 'youku':{},
    # 'Iqiyi':{},
    # '汽车之家':{},
    # '新东方':{},
    # 'QQ':{},
    # },
    # }
    # },
    # '上海':{
    # '浦东':{
    # '陆家嘴':{
    # 'CICC':{},
    # '高盛':{},
    # '摩根':{},
    # },
    # '外滩':{},
    # },
    # '闵行':{},
    # '静安':{},
    # },
    # '山东':{
    # '济南':{},
    # '德州':{
    # '乐陵':{
    # '丁乌镇':{},
    # '城区':{},
    # },
    # '平原':{},
    # },
    # '青岛':{},
    # },
    # }
    # current_layer=menu
    # while 1:
    # for key in current_layer:
    # print(key)
    # choice=input('>>>').strip()
    # if len(choice)==0: #len(choice)取choice的长度
    # continue
    # if choice in current_layer:
    # current_layer=current_layer[choice]
    # else:
    # print('无此项')


    #此时能实现四级的循环
    #加返回上一级 ,返回有问题
    # menu={
    # '北京':{
    # '朝阳':{
    # '国贸': {
    # 'CICC':{},
    # 'HP':{},
    # '渣打银行':{},
    # 'CCTV':{},
    # },
    # '望京': {
    # '陌陌':{},
    # '奔驰':{},
    # '360':{},
    # },
    # '三里屯':{
    # '优衣库':{},
    # 'apple':{},
    # },
    # },
    # '昌平':{
    # '沙河':{
    # '老男孩':{},
    # '阿泰包子':{},
    # },
    # '天通苑':{
    # '链家':{},
    # '我爱我家':{},
    # },
    # '回龙观':{},
    # },
    # '海淀':{
    # '五道口':{
    # '谷歌':{},
    # '网易':{},
    # '搜狐':{},
    # '搜狗':{},
    # '快手':{},
    # },
    # '中关村':{
    # 'youku':{},
    # 'Iqiyi':{},
    # '汽车之家':{},
    # '新东方':{},
    # 'QQ':{},
    # },
    # }
    # },
    # '上海':{
    # '浦东':{
    # '陆家嘴':{
    # 'CICC':{},
    # '高盛':{},
    # '摩根':{},
    # },
    # '外滩':{},
    # },
    # '闵行':{},
    # '静安':{},
    # },
    # '山东':{
    # '济南':{},
    # '德州':{
    # '乐陵':{
    # '丁乌镇':{},
    # '城区':{},
    # },
    # '平原':{},
    # },
    # '青岛':{},
    # },
    # }
    # current_layer=menu
    # parent_layer=menu
    # while 1:
    # for key in current_layer:
    # print(key)
    # choice=input('>>>').strip()
    # if len(choice)==0: #len(choice)取choice的长度
    # continue #执行continue后,会不执行以下的程序,去for循环那里执行程序
    # if choice in current_layer:
    # parent_layer=current_layer #保留当前层,以便返回上一层使用
    # current_layer=current_layer[choice] #进入下一层,执行完这一句,再去执行for循环
    # elif choice=='b':
    # current_layer=parent_layer
    # else:
    # print('无此项')


    #此时能实现四级的循环
    #加返回上一级
    #修改
    menu={
    '北京':{
    '朝阳':{
    '国贸': {
    'CICC':{},
    'HP':{},
    '渣打银行':{},
    'CCTV':{},
    },
    '望京': {
    '陌陌':{},
    '奔驰':{},
    '360':{},
    },
    '三里屯':{
    '优衣库':{},
    'apple':{},
    },
    },
    '昌平':{
    '沙河':{
    '老男孩':{},
    '阿泰包子':{},
    },
    '天通苑':{
    '链家':{},
    '我爱我家':{},
    },
    '回龙观':{},
    },
    '海淀':{
    '五道口':{
    '谷歌':{},
    '网易':{},
    '搜狐':{},
    '搜狗':{},
    '快手':{},
    },
    '中关村':{
    'youku':{},
    'Iqiyi':{},
    '汽车之家':{},
    '新东方':{},
    'QQ':{},
    },
    }
    },
    '上海':{
    '浦东':{
    '陆家嘴':{
    'CICC':{},
    '高盛':{},
    '摩根':{},
    },
    '外滩':{},
    },
    '闵行':{},
    '静安':{},
    },
    '山东':{
    '济南':{},
    '德州':{
    '乐陵':{
    '丁乌镇':{},
    '城区':{},
    },
    '平原':{},
    },
    '青岛':{},
    },
    }
    current_layer=menu #实现动态循环 此时current_layer={'北京':{},'上海':{},'山东':{},} 山东最后一个逗号加不加都可以
    parent_layers=[] #新建一个列表 保留上一级
    while 1:
    for key in current_layer:
    print(key)
    choice=input('>>>').strip()
    if len(choice)==0: #len(choice)取choice的长度
    continue #执行continue后,会不执行以下的程序,去for循环那里执行程序
    if choice in current_layer:
    parent_layers.append(current_layer) #把current_layer添加到parent_layers的最后,保留当前层
    current_layer=current_layer[choice] #进入下一层,执行完这一句,再去执行for循环
    elif choice=='b':
    if parent_layers: #如果列表不为空,则进入if循环
    current_layer=parent_layers.pop() #删除并返回最后一个值给current_laers,或者是取出列表的最后一个给curren_layer,返回上一级
    else:
    print('无此项')


    ******文件的读写三种模式笔记******
    #Author:"haijing"
    #date:2018/7/7

    #能调用方法的一定是对象 文件也是一个对象
    #在dany08文件夹下新建一个file Text文件 里面可以是一首诗
    #怎么样去读小童山这个文件,用open()方法

    #方法一
    # data=open('小童山','r',encoding='utf8').read() #'r'表示读模式
    # print(data) #打印小童山中的文字

    #方法二
    # f=open('小童山','r',encoding='utf8') #'r'表示读模式
    # data=f.read()
    #data=f.read(5) #5表示取小童山中5个字符,一个汉字对应一个字符,一个英文字母对应一个字符
    # print(data) #打印小童山中的文字
    #f.close() #关闭小童山这个文件 只要打开一个文件,就一定要关闭,即一定要有这一句


    #怎么样往小童山这个文件去写东西,用write()方法

    #方法一
    # data=open('小童山','w',encoding='utf8').write() #'w'表示写模式 此时已清空原文件中的内容
    # print(data) #打印小童山中的文字

    #方法二
    # f=open('小童山','w',encoding='utf8') #'w'表示写模式
    #f.write('hello world') #但是这个写,会首先把文件中原有的东西清空后,再写入hello world
    #f.write('haijing') 执行完以上三句后,小童山原有的内容会被清空,
    # 并且现在的内容是hello worldhaijing,中间没有空格

    #f.write('hello world ')
    #f.write('haijing') 此时hello world和haijing之间不在同一行
    #f.close() #关闭小童山这个文件 只要打开一个文件,就一定要关闭,即一定要有这一句


    #去写一个文件,但是又不想破坏原文件中的内容,用'a'模式
    f=open('小童山2','a',encoding='utf8') #append模式 在最后添加
    f.write(' hello world ') #第一个 是和小童山原文件中的内容换行,第二个 是为了和下一个换行
    f.write('haijing')
    f.close()

    那么 晚安 我和我的张敏
    2018.7.7 晚23:56









  • 相关阅读:
    Javascript Promise技术
    什么是CPS
    移动端input file 提示没有应用可执行此操作
    黄聪:wordpress+Windows下安装Memcached服务及安装PHP的Memcached扩展
    黄聪:Windows下安装Memcached服务及安装PHP的Memcached扩展
    用VScode配置Python开发环境
    在VSCode中使用码云(Gitee)进行代码管理
    网页链接分享到微信朋友圈带图标和摘要的完美解决方法
    解决百度统计被刷广告的办法,屏蔽非法广告
    Visual Studio代码PHP Intelephense继续显示不必要的错误
  • 原文地址:https://www.cnblogs.com/YiYA-blog/p/9278878.html
Copyright © 2020-2023  润新知