python基础(一)
一、python格式化输出
__author__ = 'meng' name = input("name:") age = int(input("age:")) job = input("job:") salary = input("salary:") info = ''' --------- info of %s ------- Name:%s Age:%d Job:%s Salary:%s ''' %(name,name,age,job,salary) print (info) info2 =''' --------- info of {_name} -------- Name:{_name} Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name, _age=age, _job=job, _salary=salary) print (info2)
while循环语句,continue,break解析
while循环,判断条件一直为True,当判断条件为flase,循环结束。
continue跳出当前循环,继续执行下一次循环。
break跳出整个循环。
三级菜单实例
__author__ = 'meng' data = { "北京":{"昌平":{"沙河":["oldboy","test"], "天通苑":["链家地产","我爱我家"] }, "朝阳":{"望京":["国贸","东直门"], "国贸":["CICC","HP"], "东直门":["Advent","飞信"] }, "海淀":{}, }, "山东":{"德州":{}, "青岛":{}, "济南":{}, }, "广东":{"东莞":{}, "常熟":{}, "佛山":{}, } } exit_flag = False while not exit_flag: for i in data: print (i) choice = input("选择进入1>>>:") if choice in data: while not exit_flag: for i2 in data[choice]: print ("\t",i2) choice2 = input("选择进入2>>>:") if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print ("\t\t",i3) choice3 = input("选择进入3>>>:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print ("\t\t\t",i4) choice4 = input("最后一层,按 b 返回>>:") if choice4 == "b": pass elif choice4 == "q": exit_flag = True if choice3 == "b": break elif choice3 == "q": exit_flag = True if choice2 == "b": break elif choice2 == "q": exit_flag = True
用函数实现
__author__ = 'meng' #!coding:utf-8 data = { "北京":{"昌平":{"沙河":["oldboy","test"], "天通苑":["链家地产","我爱我家"] }, "朝阳":{"望京":["国贸","东直门"], "国贸":["CICC","HP"], "东直门":["Advent","飞信"] }, "海淀":{}, }, "河南":{"漯河":{"召陵区":["许慎墓","贾湖遗址"], "郾城区":["漯河四高","小商桥"]}, "郑州":{"二七区":["二七纪念塔","二七广场"], "中原区":["金水河","索河"]}, "洛阳":{"老城区":["龙门石窟","牡丹花"]}, }, "广东":{"东莞":{}, "常熟":{}, "佛山":{}, } } dic_key = {} def menmus_1(): #一级菜单 print ('--------------- 一级菜单 ------------------') for i,k in enumerate(data,1): # 遍历一级菜单,打印一级菜单的下标 print(i,k) dic_key[str(i)] = k #key不存在,创建一个key-value choose = input('输入序号选择一级菜单,输入B返回上一级菜单,输入Q直接退出!>>>').strip() if choose == 'b': print ('这是一级菜单不能返回上一级,') elif choose == 'q': exit() elif dic_key.get(choose): menmus_2(dic_key[choose]) else: print ('输入错误,请尝试在输入一次') def menmus_2(choose_1): #二级菜单 print ('-------------- 二级菜单 ------------------') for i,k in enumerate(data[choose_1],1): print (i,k) dic_key[str(i)] = k choose_2 = input('输入序号选择二级菜单,输入B返回上一级菜单,输Q直接退出!>>>').strip() if choose_1 == 'b': menmus_1() elif choose_1 == 'q': exit() elif dic_key.get(choose_2): menmus_3(choose_1,dic_key[choose_2]) else: print ('输入错误,请尝试在输入一次') def menmus_3(choose_1,choose_2): #三级菜单 print ('-------------- 三级菜单 ------------------') for i,k in enumerate(data[choose_1][choose_2],1): print (i,k) dic_key[str(i)] = k choose_3 = input('输入序号选择三级菜单,输入B返回上一级菜单,输Q直接退出!>>>').strip() if choose_3 == 'b': menmus_2(choose_1) elif choose_3 == 'q': exit() elif dic_key.get(choose_3): menmus_4(choose_1,choose_2,dic_key[choose_3]) else: print ('输入错误,请尝试在输入一次') def menmus_4(choose_1,choose_2,choose_3): for i ,k in enumerate(data[choose_1][choose_2][choose_3],1): print (i,k) while True: menmus_1()
吃鸡id:开车撞死一群人