day2
Posted Felix运维
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day2相关的知识,希望对你有一定的参考价值。
三级菜单:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#user:Felix
#date:2016-10-19
#mail:wudonghang@wudonghang.com
#If you have questions or suggestions, please contact the author.
china_map ={
"华南":{
"广东":["广州市","佛山市","深圳市","东莞市"],
"广西":["南宁市","柳州市","桂林市","北海市"],
"海南":["海口市","三亚市","三沙市","儋州市"]
},
"华东":{
"上海":["黄浦区","卢湾区","徐汇区","长宁区","普陀区"],
"安徽":["合肥市","芜湖市","淮南市","马鞍山市"],
"江苏":["南京市","无锡市","徐州市","常州市","苏州市"]
},
"华北":{
"北京":["东城区","西城区","朝阳区","丰台区","石景山区","海淀区"],
"山西":["太原市","大同市","阳泉市","长治市"],
"河北":["石家庄市","唐山市","秦皇岛市","邢台市"]
},
"华中":{
"湖北":["武汉市","黄石市","十堰市","十堰市"],
"河南":["郑州市","开封市","洛阳市","平顶山市"],
"湖南":["长沙市","株洲市","衡阳市","邵阳市"]
},
"西南":{
"重庆":["万州区","涪陵区","渝中区","大渡口区"],
"四川":["成都市","自贡市","攀枝花市","德阳市"],
"贵州":["贵阳市","六盘水市","遵义市","安顺市"],
},
"特别行政区":{
"香港":["屯门","弯仔","北角","西贡"],
"澳门":["花地玛堂区","圣安多尼堂区","大堂区","望德堂区"],
},
}
print("".center(50,"-"))
print("+ +".center(50))
print("+ +".center(50))
print("+ 中华人民共和国查询系统 +".center(40))
print("+ +".center(50))
print("+ +".center(50))
print("".center(50,"-"))
print("大中华地区一级划分:")
while True:
for i in china_map:
print(i)
print("".center(50,"-"))
greater_china_name = input("请输入你要查看的中华地区名字 or 按q:Exit是退出整个程序:")
if greater_china_name in china_map:
gc_name = china_map[greater_china_name]
province_name = gc_name.keys()
while True:
print("------------------包含的省名字二级:-----------------")
for i in province_name:
print(i)
print("".center(50,"-"))
sheng_name_input = input("请输入你要查看的省名字 or 按b:Back是返回上一级菜单;按q:Exit是退出整个程序:")
if sheng_name_input in province_name:
shi_name = china_map[greater_china_name][sheng_name_input]
print("--------------包含的城市名三级:-------------------")
for i in shi_name:
print(i)
else:
print("".center(50,"-"))
shi_name_input = input("按b:Back是返回上一级菜单;按q:Exit是退出整个程序:")
if shi_name_input == "b":
continue
elif shi_name_input == "q":
exit("退出中华人民共和国查询系统".center(50,"-"))
else:
print("输入的指令不对请重新输入!")
elif sheng_name_input == "b":
break
elif sheng_name_input == "q":
exit("退出中华人民共和国查询系统".center(50,"-"))
else:
print("输入的省名字不对请重新输入!")
elif greater_china_name == "q":
exit("退出中华人民共和国查询系统".center(50,"-"))
else:
print("输入的地区名字不对请重新输入!")
三级菜单文艺青年版
menu = {
\'北京\':{
\'海淀\':{
\'五道口\':{
\'soho\':{},
\'网易\':{},
\'google\':{}
},
\'中关村\':{
\'爱奇艺\':{},
\'汽车之家\':{},
\'youku\':{},
},
\'上地\':{
\'百度\':{},
},
},
\'昌平\':{
\'沙河\':{
\'老男孩\':{},
\'北航\':{},
},
\'天通苑\':{},
\'回龙观\':{},
},
\'朝阳\':{},
\'东城\':{},
},
\'上海\':{
\'闵行\':{
"人民广场":{
\'炸鸡店\':{}
}
},
\'闸北\':{
\'火车战\':{
\'携程\':{}
}
},
\'浦东\':{},
},
\'山东\':{},
}
current_level = menu
last_levels = []
while True:
for key in current_level:
print(key)
choice = input(">>>>:").strip()
if choice == "b":
if not last_levels:break
current_level = last_levels[-1]
last_levels.pop()
elif choice == "q":
exit("quit")
elif choice not in current_level:
continue
else:
last_levels.append(current_level)
current_level = current_level[choice]
购物车:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#user:Felix
#date:2016-10-19
#mail:wudonghang@wudonghang.com
#If you have questions or suggestions, please contact the author.
import os
import json
import time
import msvcrt
#定义购物车系统使用变量
data_file = "data.db"
system_start = "系统初始化中......".center(50,"-")
system_1 = "正在加载系统数据库...".center(50,"-")
system_2 = "系统数据库加载成功!".center(50,"-")
system_3 = "系统初始化完成!".center(50,"-")
system_error_1 = "系统数据库加载失败,请联系管理员!".center(50,"-")
system_error_2 = "系统数据库为空,请联系管理员!".center(45,"-")
system_error_3 = "用户已被锁定,请联系管理员!".center(45,"-")
system_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
shop_list = [["Ipone6",5000],["Ipone7",6000],["Mac Pro",15800],["Coffee",30],["Bike",2000],["Cloth",500],["Note Book",10]]
def user_message():
with open(data_file,\'r\') as message:
user_message = json.load(message)
return user_message
def client_information():
import socket
import getpass
import platform
import uuid
myname1=socket.getfqdn(socket.gethostname())
myaddr=socket.gethostbyname(myname1)
user=getpass.getuser()
myplatform=platform.platform()
mac=uuid.UUID(int=uuid.getnode())
MAC=mac.hex[-12:]
information = "主机名:" + myname1 + " " + "IP:" + myaddr + " " + "用户:" + user + " " + "系统:" + myplatform + " " + "MAC:" + MAC
return information
def system_initialization():
print("%s\\n%s"%(system_start,system_1))
if os.path.exists(data_file):
print(system_2)
if open(data_file,"r").read() == "":
exit(system_error_2)
else:
exit(system_error_1)
print("当前系统时间: %s".center(35,"-") %(system_time))
print(system_3)
def password_write():
print(\'请输入密码: \', end=\'\', flush=True)
li = []
while 1:
ch = msvcrt.getch()
if ch == b\'\\r\':
msvcrt.putch(b\'\\n\')
password = b\'\'.join(li).decode()
break
elif ch == b\'\\x08\':
if li:
li.pop()
msvcrt.putch(b\'\\b\')
msvcrt.putch(b\' \')
msvcrt.putch(b\'\\b\')
elif ch == b\'\\x1b\':
break
else:
li.append(ch)
msvcrt.putch(b\'*\')
return password
def user_authentication(user_message):
username = input("请输入用户名:")
if username not in user_message:
exit("系统没有找到这个用户".center(50,"-"))
real_password = user_message[username][0][\'password\']
if int(user_message[username][0][\'lock\']) == 0:
exit(system_error_3)
count = 0
while True:
print("请选择 0:密码明文输入 1:密码加密输入 ---提示密码加密需要用终端运行!")
case=input(">>>")
if case.isdigit():
case=int(case)
else:
print("\\033[0;31;40m请输入数字\\033[0m".center(50,"-"))
if case == 0:
password=input("请输入密码: ")
elif case == 1:
password = password_write()
else:
print("\\033[0;31;40m请输入有效指令ID\\033[0m")
if password == real_password:
print("Welcome! %s \\n您的余额为:%s"%(username,user_message[username][0][\'salary\']))
print("您上次的登陆信息为:%s"%user_message[username][0][\'client_information\'])
user_message[username][0][\'client_information\']=client_information()
json.dump(user_message, open(data_file, \'w\'))
break
elif count < 2:
print("用户密码输入不正确,请重新输入密码:")
count += 1
continue
else:
user_message[username][0][\'lock\'] = 0
json.dump(user_message, open(data_file, \'w\'))
exit("您输入错误的密码超过3次,系统已将您的账户锁定".center(50,"-"))
return username
def shoping(username,user_message):
shopping_list = []
history_shopping = user_message[username][0][\'history_shopping\']
last_shoppoing = user_message[username][0][\'last_shopping\']
history_shopping.extend(last_shoppoing)
user_message[username][0][\'history_shopping\']=history_shopping
while True:
print("product list".center(50,"-"))
for index,i in enumerate(shop_list):
print(index,i[0],i[1])
choice = input("请输入商品编号 or [%s]\\n>>>:"%("quit"))
if choice.isdigit():
choice=int(choice)
if choice >=0 and choice < len(shop_list):
p = shop_list[choice]
if user_message[username][0][\'salary\'] >= p[1]:
user_message[username][0][\'salary\'] -= p[1]
shopping_list.append(p)
print("Added \\033[32;1m[%s]\\033[0m into your shopping cart,and your current balance is \\033[41;1m%s\\033[0m" %(p[0],user_message[username][0][\'salary\']))
else:
print("钱不够,你只有[\\033[41;1m%s\\033[0m]"%user_message[username][0][\'salary\'])
else:
print("\\033[0;31;40m没有此类商品...\\033[0m")
elif choice == "quit":
print("本次已购买商品".center(50,"-"))
a = []
new_shopping = []
for i in shopping_list:
b = i[0] + " x " + str(shopping_list.count(i)) + " \\033[1;31;40m总价\\033[0m: " + str(shopping_list.count(i) * i[1])
a.append(b)
for i in a:
if i not in new_shopping:
new_shopping.append(i)
for i in new_shopping:
print(i)
print("Your left balance is ", user_message[username][0][\'salary\'])
user_message[username][0][\'last_shopping\'] = new_shopping
json.dump(user_message, open(data_file, \'w\'))
exit("购物车程序已经退出!".center(50,"-"))
def menu(user_message):
while True:
case = input("请选择 0:注册 1:登陆 2:查询 3:充值 4:修改密码 5:解除锁定 6:退出\\n>>>")
if case.isdigit():
case=int(case)
else:
print("\\033[0;31;40m请输入数字\\033[0m".center(50,"-"))
if case == 0:
print("开始注册".center(50,"-"))
username=input("请输入用户名:")
if username in user_message:
print("用户名重复".center(50,"-"))
continue
else:
user_message[username]=[{\'history_shopping\':[],\'password\':\'123\',\'last_shopping\':[],\'client_information\':[],\'salary\':0,\'lock\':1}]
print(user_message,user_message[username])
json.dump(user_message, open(data_file, \'w\'))
print("用户注册成功!%s 默认密码为:123 初始余额为:0"%username)
elif case == 1:
shoping(username=user_authentication(user_message=user_message),user_message=user_message)
elif case == 2:
print("开始查询".center(50,"-"))
username=input("请输入要查询的用户:")
if username not in user_message:
print("系统没有找到这个用户".center(50,"-"))
continue
last_shopping=str(user_message[username][0][\'last_shopping\'])
history_shopping=str(user_message[username][0][\'history_shopping\'])
print("用户名:%s\\n上次购买的商品为:%s\\n历史购买的商品为:%s\\n上次登陆信息为:%s\\n余额为:\\033[41;1m%s\\033[0m"%(username,last_shopping.strip("[").strip("]").replace("\\\\x1b[1;31;40m","").replace("\\\\x1b[0m",""),history_shopping.strip("[").strip("]").replace("\\\\x1b[1;31;40m","").replace("\\\\x1b[0m",""),user_message[username][0][\'client_information\'],user_message[username][0][\'salary\']))
elif case == 3:
print("开始充值".center(50,"-"))
username=input("请输入要充值的用户:")
print("您当前的余额为:\\033[41;1m%s\\033[0m"%(user_message[username][0][\'salary\']))
money=input("请输入要充值的金额:")
if money.isdigit():
money=int(money)
user_message[username][0][\'salary\']+=money
print("充值成功!%s 当前的余额为:\\033[41;1m%s\\033[0m"%(username,user_message[username][0][\'salary\']))
json.dump(user_message, open(data_file, \'w\'))
else:
exit("输入错误,为了账号安全,系统已强制退出,欢迎您下次光临".center(50,"-"))
elif case == 4:
username=input("请输入要修改密码的用户:")
if username not in user_message:
print("系统没有找到这个用户".center(50,"-"))
continue
else:
old_passwd = input("请输入老密码:")
if old_passwd == user_message[username][0][\'password\']:
new1_passwd = input("请输入新密码:")
new2_passwd = input("请再次输入新密码:")
if new1_passwd == new2_passwd:
user_message[username][0][\'password\']=new2_passwd
json.dump(user_message, open(data_file, \'w\'))
print("修改密码完成!")
continue
else:
print("两次输入的密码不一致!")
continue
else:
print(以上是关于day2的主要内容,如果未能解决你的问题,请参考以下文章