三级菜单:

#!/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("密码校验不对,请确认财产安全的情况下,再次修改密码!")
elif case == 5:
username=input("请输入要解除锁定的用户:")
passwd = input("请输入已知密码:")
if username not in user_message:
print("系统没有找到这个用户".center(50,"-"))
continue
elif passwd == user_message[username][0]['password']:
user_message[username][0]['lock']=1
json.dump(user_message, open(data_file, 'w'))
print("用户[%s]已经解除锁定!"%username)
else:
print("密码校验不对,请确认财产安全的情况下,再次解除锁定!")
elif case == 6:
exit("已经退出购物车系统".center(50,"-"))
else:
print("\033[0;31;40m请输入有效指令ID\033[0m")

system_initialization()
menu(user_message=user_message())
data.db:
{"wudonghang": [{"lock": 1, "history_shopping": ["Note Book x 2 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 20", "Coffee x 2 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 60", "Ipone6 x 2 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 10000", "Cloth x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 500", "Note Book x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 10", "Coffee x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 30", "Bike x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 2000", "Coffee x 2 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 60", "Bike x 2 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 4000", "Cloth x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 500", "Note Book x 5 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 50", "Coffee x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 30"], "client_information": "\u4e3b\u673a\u540d\uff1aYOS-01606252229 IP\uff1a192.168.11.11 \u7528\u6237\uff1aAdministrator \u7cfb\u7edf\uff1aWindows-7-6.1.7601-SP1 MAC\uff1a000b2f40160f", "salary": 23312, "last_shopping": ["Note Book x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 10", "Cloth x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 500"], "password": "123"}], "liuyao": [{"lock": 1, "history_shopping": ["Coffee x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 30", "Ipone6 x 1 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 5000"], "client_information": "\u4e3b\u673a\u540d\uff1aYOS-01606252229 IP\uff1a192.168.11.11 \u7528\u6237\uff1aAdministrator \u7cfb\u7edf\uff1aWindows-7-6.1.7601-SP1 MAC\uff1a000b2f40160f", "salary": 19465, "last_shopping": ["Coffee x 2 \u001b[1;31;40m\u603b\u4ef7\u001b[0m: 60"], "password": "123"}]}
readme:
###博客园地址:[Python之路,Day2 - Python基础2](http://www.cnblogs.com/wudonghang/p/5984808.html "我的博客园地址")
***
###程序简介:
>背景

#####作业:

* 购物车

* 三级菜单

***
>购物车构想

包含 0:注册 1:登陆 2:查询 3:充值 4:修改密码 5:解除锁定 6:退出等功能

采用json数据格式完成数据持久化
0:注册 采用json数据新增
1:登陆 采用三次验证规则,先判断有没有这个用户,然后判断是否是锁定状态,然后验证密码,错误超过三次锁定用户并退出系统
2:查询 将用户上次购买,历史购买,上次登陆信息以及余额显示出来
3:充值 将充值的金额与json中数据相加出新值
4:修改密码 判断old密码是否正确,错误退出。正确判断输入2次密码是否一致,一致写入文件
5:解除锁定 判断用户以及密码,来修改json中lock值
6:退出 exit

***

***
>三级菜单构想

1.定义一个字典,定义地区,省,市
2.嵌套2个 while True循环,一个为地区外循环,另一个为省市内循环
3.判断用户输入指令是否正确,不正确报错重新输入
4.判断用户输入是正确指令还是“b”or“q”,b:返回上一级 q:退出程序

***






day2的更多相关文章

  1. 【从零开始学BPM,Day2】默认表单开发

    [课程主题]主题:5天,一起从零开始学习BPM[课程形式]1.为期5天的短任务学习2.每天观看一个视频,视频学习时间自由安排. [第二天课程] Step 1 软件下载:H3 BPM10.0全开放免费下 ...

  2. NOIp2016 Day1&Day2 解题报告

    Day1 T1 toy 本题考查你会不会编程. //toy //by Cydiater //2016.11.19 #include <iostream> #include <cstd ...

  3. java day2一个模拟双色球的代码

    package day2; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt ...

  4. Python基础-day2

    1.Python模块python 中导入模块使用import语法格式:import module_name示例1: 导入os模块system('dir')列出当前目录下的所有文件 # _*_ codi ...

  5. 【BZOJ 4517】【SDOI 2016 Round1 Day2 T2】排列计数

    本蒟蒻第一次没看题解A的题竟然是省选$Round1$ $Day2$ $T2$ 这道组合数学题. 考试时一开始以为是莫队,后来想到自己不会组合数的一些公式,便弃疗了去做第三题,,, 做完第三题后再回来看 ...

  6. 冲刺阶段 day2

    day2 项目进展 今天本组五位同学聚在一起将项目启动,首先我们对项目进行了规划,分工,明确指出每个人负责哪些项目.由负责第一部分的组员开始编程,在已经搭建好的窗体内,对系部设置进行了编写,本校共六个 ...

  7. python_way ,day2 字符串,列表,字典,时间模块

    python_way ,day2 字符串,列表,字典,自学时间模块 1.input: 2.0 3.0 区别 2.0中 如果要要用户交互输入字符串: name=raw_input() 如果 name=i ...

  8. 二模 (8) day2

    第一题: 题目描述: 有 n 个炸弹,有些炸弹牵了一根单向引线(也就是说引线只有在这一端能被炸弹点燃),只要引爆了这个炸弹,用引线连接的下一个炸弹也会爆炸.每个炸弹还有个得分,当这个炸弹被引爆后就能得 ...

  9. Spark菜鸟学习营Day2 分布式系统需求分析

    Spark菜鸟学习营Day2 分布式系统需求分析 本分析主要针对从原有代码向Spark的迁移.要注意的是Spark和传统开发有着截然不同的思考思路,所以我们需要首先对原有代码进行需求分析,形成改造思路 ...

随机推荐

  1. 自己动手写插件底层篇—基于jquery移动插件实现

    序言 本章作为自己动手写插件的第一篇文章,会尽可能的详细描述一些实现的方式和预备知识的讲解,随着知识点积累的一点点深入,可能到了后期讲解也会有所跳跃.所以,希望知识点不是很扎实的读者或者是初学者,不要 ...

  2. jquery-leonaScroll-1.2-自定义滚动条插件

    leonaScroll-1.2.js 下载链接地址:http://share.weiyun.com/bb531dd6b1916c0023c176897182dc15 (密码:iZck)[内含压缩版] ...

  3. 使用antd UI 制作菜单

    antd 主页地址:https://ant.design/docs/react/introduce 在使用过程中,不能照搬antd的组件代码,因为有些并不合适.首先,菜单并没有做跳转功能,仅仅是菜单, ...

  4. PHP "延迟静态绑定" 功能,static

    从这个名字的定义提取出两个关键点,第一点静态,也就是说这个功能只适用于静态属性或静态方法.第二点延迟绑定,这个根据下面代码就可以很好的理解 看一下这个例子: class A{ static $name ...

  5. APiCloud真机调试需要注意的几个问题

    具体请看官方文档:http://docs.apicloud.com/Dev-Tools/wifi-debug. APiCloud Android手机真机调试,需要首先在手机上安装官网提供的apploa ...

  6. Ubuntu16.04编译Android6.0/cm13.0教程及相关错误解决办法

    一.必备工作 1.安装依赖库 sudo apt--dev libesd0-dev git-core gnupg flex bison gperf build-essential zip curl zl ...

  7. extracting fasta records from a multi-fasta file based on a list using awk

    for i in $(cat gene_list) do awk -v RS=">" '($1==a){print ">"$0}' a=$i inp ...

  8. 关于 DataGridTextColumn的IsReadOnly

    1. 以下是绑定方式,但是IsReadOnly不起作用 <DataGrid x:Name="dgTest" ItemsSource="{Binding}" ...

  9. wpf converter converterparameter 绑定多参数

    1. converterparameter不是依赖属性,所以不能用binding. 2. 可以把converter 的接口 IValueConverter改为 IMultiValueConverter ...

  10. JavaScript 中一些概念理解 :clientX、clientY、offsetX、offsetY、screenX、screenY

    clientX 设置或获取鼠标指针位置相对于窗口客户区域的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. clientY 设置或获取鼠标指针位置相对于窗口客户区域的 y 坐标,其中客户区域不包 ...