作业一: 三级菜单
要求:
打印省、市、县三级菜单
可返回上一级
可随时退出程序
menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车战': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}
# tag = True
# while tag:
# city = input("""
# 在任何时候请按Q退出,按B返回上一层(不区分大小写)
# 本层可用指令有:{cmd}
# 请输入指令:
# """.format(cmd=" ".join(menu.keys()))).strip()
# if city == "":
# print("您没有输入指令,请重新输入")
# continue
# if city.upper() == "B":
# print("已经是最上层了,无法再向上了")
# continue
# if city.upper() == "Q":
# tag = False
# break
# if city not in menu:
# print("输错了")
# continue
# print("\n".join(menu[city]))
# if len(menu[city]) != 0:
#
# while tag:
# city2 = input("""
# 在任何时候请按Q退出,按B返回上一层(不区分大小写)
# 本层可用指令有:{cmd}
# 请输入指令:
# """.format(cmd=" ".join(menu[city].keys()))).strip()
# if city2 == "":
# print("您没有输入指令,请重新输入")
# continue
# if city2.upper() == "B":
# print("操作已取消")
# break
# if city2.upper() == "Q":
# tag = False
# break
# if city2 not in menu[city]:
# print("输错了")
# continue
# print("\n".join(menu[city][city2]))
# if len(menu[city][city2]) != 0:
# while tag:
# city3 = input("""
# 在任何时候请按Q退出,按B返回上一层(不区分大小写)
# 本层可用指令有:{cmd}
# 请输入指令:
# """.format(cmd=" ".join(menu[city][city2].keys()))).strip()
# if city3 == "":
# print("您没有输入指令,请重新输入")
# continue
# if city3.upper() == "B":
# print("操作已取消")
# break
# if city3.upper() == "Q":
# tag = False
# break
# if city3 not in menu[city][city2]:
# print("输错了")
# continue
# print("\n".join(menu[city][city2][city3]))
# else:
# print("下层为空了")
# else:
# print("下层为空了")
l=[menu,]
while True:
print("当前层:{c}".format(c=l[-1]))
cc=l[-1]
for key in l[-1]:
print(key)
t=input(">>:")
if t not in cc:continue
print(cc[t])
l.append(cc[t])
print("添加后%s"%l)
#
# layers = [menu, ]
#
# while True:
# current_layer = layers[-1]
# for key in current_layer:
# print(key)
#
# choice = input('>>: ').strip()
#
# if choice not in current_layer: continue
#
# layers.append(current_layer[choice])
作业二:请闭眼写出购物车程序
需求:
用户名和密码存放于文件中,格式为:egon|egon123
启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
product = [
["1", "农夫山泉", 5], ["2", "老干妈", 20], ["3", "怡宝", 8], ["4", "老坛酸菜", 15], ["5", "鸡排", 35]
]
err_num = 0
select_product = []
tag = True
while tag:
if err_num >= 3:
print("你已连续输错三次了,别试了")
tag = False
break
user_name = input("请输入用户名:").strip()
password = input("请输入密码:").strip()
with open("db.txt", 'r', encoding='utf-8') as f:
for user in f:
user = user.strip()
user = user.split("|")
if user_name == user[0] and password == user[1]:
print("登陆成功")
while tag:
rmb = input("请输入工资(或输入b退出):").strip()
if rmb == "b":
tag = False
break
if rmb.replace(".", "", 1).isdigit():
user_info = [user_name, int(rmb)]
print("商品列表".center(50, "-"))
print("商品ID\t商品名\t\t价格")
for i in product:
p_id, k, v = i
print("{id}\t\t{p_name}\t\t{price}".format(id=p_id, p_name=k, price=v))
while tag:
input_p_id = input("请输入要购买的商品编号(或输入b退出):").strip()
if input_p_id in ["b"]:
tag = False
break
for i in product:
if input_p_id in i:
product_select_isok = input("确定要购买《{p_name}》吗?".format(p_name=i[1]))
if product_select_isok in ["y", "Y", "YES", "yes"]:
if i[2] <= int(user_info[1]):
for k in select_product:
if k[0] == i[0]:
k[3] += 1
break
else:
select_product.append(i)
select_product[-1].append(1)
print("已添加到购物车,请继续购物")
user_info[1] = int(user_info[1]) - i[2]
else:
print("钱不够,购买失败,当前余额为{mount}".format(mount=user_info[1]))
add_mount = input("请输入充值金额或按b退出程序:").strip()
if add_mount == "b":
tag = False
break
if add_mount.replace(".", "", 1).isdigit():
user_info[1] += int(add_mount)
print("您的帐户余额为{mount},请继续购物".format(mount=user_info[1]))
else:
print("请重新选择商品")
break
else:
print("商品编号不存在,请重新输入")
else:
print("金额不正确,请重新输入")
break
else:
print("登陆失败,请重试")
err_num += 1
print("已购商品".center(50, "-"))
if len(select_product):
print("商品ID\t\t商品名称\t\t单价\t\t数量\t\t总价")
for buy_product in select_product:
print("{p_id}\t\t\t{pro_name}\t\t{price}\t\t\t{num}\t\t\t{sum_price}".format(p_id=buy_product[0],
pro_name=buy_product[1],
price=buy_product[2],
num=buy_product[3],
sum_price=int(
buy_product[2]) * int(
buy_product[3])))
else:
print("购物车是空的,你这个穷鬼")
99乘法表
for i in range(1,10):
for j in range(i,10):
print('%s*%s=%s'%(i,j,i*j),end="\t")
print("\r")
for i in range(9,-1,-1):
for j in range(i,9):
print('%s*%s=%s'%(i+1,j+1,(i+1)*(j+1)),end="\t")
print("\r")
for i in range(0,10):
for j in range(9,i,-1):
print('%s*%s=%s'%(i+1,j,(i+1)*j),end="\t")
print("\r")
for i in range(9,-1,-1):
for j in range(9,i,-1):
print('%s*%s=%s'%(i+1,j,(i+1)*j),end="\t")
print("\r")