作业3:多级菜单
三级菜单
可依次选择进入各子菜单
所需新知识点、列表、字典
3级菜单 老师讲解:
#__author__'Chris Li'
data = {
'北京':{
'昌平':{
'沙河':['oldboy','test'],
'天通苑':['链家地产','我爱我家']
},
'朝阳':{
'望京':['奔驰','陌陌'],
'国贸':['CICC','HP'],
'东直门':['Advent','飞信'],
},
'海淀':{}
},
'山东':{
'德州':{},
'青岛':{},
'济南':{}
},
'广东':{
'东莞':{},
'常熟':{},
'佛山':{}
}
}
exit_flag = False
while not exit_flag:
for i in data:
print(i)
choice = input('选择进入>>:')
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
程序练习
购物车程序
需求:
1、启动程序后,让用户输入工资,然后打印商品列表;
2、允许用户根据商品编号购买商品;
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒;
4、可随时退出,退出时,打印已购买商品和余额。
自己写的:
wage = float(input('您卡内的金额:'))
product = [['Iphone' ,6800],
['Mac Pro' ,8000],
['Starbuck Latte' ,31],
['Alex Python' ,81],
['Bike' ,2000]]
shopping_cart = []
while True:
print('您的余额:' ,wage )
for n in range(len(product)):
print( n +1 ,product[n][0] ,product[n][1])
number = input("Please select the product you need or 'q' Quit shopping.")
if number == 'q':
break
# 用isdigit 函数判断是否数字
elif number.isdigit() and wage >= product[int(number)-1][1]:
print('添加 %s 进您的购物车.' % product[int(number)-1][0])
shopping_cart.append(product[int(number)-1])
wage = wage - product[int(number)-1][1]
elif wage < product[int(number)-1][1]:
print('您的余额不足。')
print('-----您购买的物品----')
for n in range(len(shopping_cart)):
print(n + 1, shopping_cart[n][0], shopping_cart[n][1])
老师讲解:
product_list = [
('Iphone',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('coffee',31),
('Python Book',100),
]
shopping_list = []
salary = input('Input your salary:')
if salary.isdigit() :
salary = int(salary)
while True:
#enumerate 把索引与元素 元组方式打印出来; for i in enumerate(product_list):print(i)
for index,item in enumerate(product_list):
print(index,item)
user_choice = input('选择要买吗?>>>:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice >= 0 and user_choice < len(product_list):
p_item = product_list[user_choice]
if p_item[1] <= salary: #买的起
shopping_list.append(p_item)
salary -= p_item[1]
print('Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m' % (p_item,salary))
# \033[31;1m xxxxxxxxxx \033[0m' 凸显字体颜色
else:
print('\033[41;1m你的余额只剩下[%s]啦,还买个毛线\033[0m' % salary)
else:
print('product code [%s] is not exit!' % user_choice)
elif user_choice == 'q':
print('-----shopping list----')
for p in shopping_list:
print(p)
print('Your current balance:',salary)
exit()
else:
print('invalid option')
else:
print('输入错误')
程序练习
购物车程序
需求:
1、启动程序后,让用户输入工资,然后打印商品列表;
2、允许用户根据商品编号购买商品;
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒;
4、可随时退出,退出时,打印已购买商品和余额。
作业:
购物车
用户入口
1.商品信息存在文件里
2.已购商品,余额记录
商家入口
1.可以添加商品,修改商品价格
商家入口
__author__ = 'Chris Li'
file = open('product_list.txt','r')
product_list = list(eval(file.read()))
file.close()
while True :
print('-----商品目录-----')
for index,item in enumerate(product_list):
print(index,item)
user_choice = input('请选择:1添加商品;2修改价格;3删除商品;4保存修改退出。')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice >= 1 and user_choice <= 4:
if user_choice == 1:
product_name = str(input('请输入商品的"名称":'))
product_cost = float(input('请输入商品的"价格":'))
product_list.append((product_name,product_cost))
elif user_choice == 2:
product_number = int(input('请输入需要修改商品的"编码":'))
modify_cost = float(input('请输入修改后的价格:'))
product_list[product_number] = (product_list[product_number][0],modify_cost)
elif user_choice == 3:
product_number = int(input('请输入需要删除商品的"编码":'))
del product_list[product_number]
elif user_choice == 4:
break
else:
print('输入错误')
else:
print('输入错误')
file = open('product_list.txt','w')
file.write(str(product_list))
file.close()
print('商品目录成功保存...')
input(' ')
用户入口
__author__ = 'Chris Li'
file = open('user_list.txt','r') #读取文档
info = dict(eval(file.read())) #转换为字典
salary = info['salary']
shopping_list= []
file.close()
file_product_list = open('product_list.txt','r')
product_list = list(eval(file_product_list.read())) #转换为列表
file_product_list.close()
print('您的余额为\033[31;1m%s\033[0m' % salary)
print('-----您曾经购买的商品-----')
for index,item in enumerate(info['product']):
print(index,item)
print('-------------------------')
if info['salary'] == 0:
while True:
salary = input('请输入您的金额:')
if salary.isdigit(): #判断是否为数字
salary = float(salary)
info['salary'] = salary
file = open('user_list.txt', 'w')
file.write(str(info))
file.close()
break
else:
print('您输入的金额有误,请重新输入:')
while True:
print('----商品列表-----')
for index,item in enumerate(product_list): #enumerate 把索引、元素 打印出来
print(index,item)
user_choice = input('选择要买吗?>>>:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice >=0 and user_choice < len(product_list):
p_item = product_list[user_choice]
if salary >= p_item[1]:
shopping_list.append(p_item)
salary -= p_item[1]
print('Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m' % (p_item,salary))
# \033[31;1m xxxxxxxxxx \033[0m' 凸显字体颜色
else:
print('\033[41;1m你的余额只剩下[%s]啦,还买个毛线\033[0m' % salary)
else:
print('product code [%s] is not exit!' % user_choice)
elif user_choice == 'q':
print('-----shopping list-----')
for p in shopping_list:
print(p)
info['product'].append(p)
print('Your current balance:', salary)
info['salary'] = salary
file = open('user_list.txt', 'w')
file.write(str(info))
file.close()
exit()
else:
print('invalid option')
文件1 - product_list.txt
[(‘Iphone’, 5800), (‘Mac Pro’, 9800), (‘Bike’, 800), (‘Watch’, 10600), (‘coffee’, 29.0), (‘Python Book’, 100)]
文件2 - user_list.txt
{‘salary’: 3800.0, ‘product’: [(‘Python Book’, 100), (‘Python Book’, 100), (‘Python Book’, 100), (‘Python Book’, 100), (‘Iphone’, 5800)]}