购物商城实践
Posted walch2006
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物商城实践相关的知识,希望对你有一定的参考价值。
1、想快点学会,唯有不断地练习。练习中严格要求。
2、题目
#启动程序后,让用户输入工资,然后打印商品列表
#允许用户根据商品编号购买商品
#用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
#可随时退出,退出时,打印已购商品和余额
3、解答过程
commodity_list = [(‘Ipone‘,6800),
(‘Tea‘,1200),
(‘Pencil‘,12),
(‘Apple‘,85),
(‘Radio‘,120),
(‘Bike‘,1800),]
salary = input(‘input your salary:‘)
shopping_list = []
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(commodity_list,start=1):
print(index,item)
user_choice = input(‘Please choice what you want ?>>>:‘)
if user_choice.isdigit():
user_choice =int(user_choice)
if user_choice >=0 and user_choice < len(commodity_list):
p_item = commodity_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 33[31;1m %s: 33 [0m‘%(p_item,salary))
else:
print(" 33[41;1mYour current salary is %s,Your money is not enough! 33[0m")
else:
print(‘Invalid option!")
elif user_choice = "q":
print("--------------- shopping list ---------------)
for p in shopping_list:
print(p)
print("Your current balance:",salary)
exit()
经过测试调整:
#准备好商品列表
commodity_list = [(‘Ipone‘,6800),
(‘Tea‘,1200),
(‘Pencil‘,12),
(‘Apple‘,85),
(‘Radio‘,120),
(‘Bike‘,1800)]
#提示输入你准备的购物款
salary = input(‘input your salary:‘)
#准备空的购物车
shopping_list = []
#isdigit()判断输入的是否为十进制数字
if salary.isdigit():
salary = int(salary)
while True:
#enmuerate()将商品列表组合为索引系列,同时列出数据下标和数据。
for index,item in enumerate(commodity_list):
#提供购物列表供选择
print(index,item)
#提示选择购物
user_choice = input(‘Please choice what you want ?>>>:‘)
if user_choice.isdigit():
user_choice =int(user_choice)
#确保在提供的购物列表范围内选择
if user_choice >=0 and user_choice < len(commodity_list):
#将顾客从购物列表中选中的商品储存到变量p_item中
p_item = commodity_list[user_choice]
#选中商品价格与购物款作比较,判断是否买得起
if p_item[1] <= salary:
#买得起就放进购物车
shopping_list.append(p_item)
#从总购物款中扣除
salary -= p_item[1]
# 33[31;1m? 33[0m 为?选择颜色
print(‘Added %s into shopping cart,your current balance is 33[31;1m%s: 33[0m‘%(p_item,salary))
else:
print(" 33[41;1mYour current salary is %s,Your money is not enough! 33[0m"%salary)
else:
#输入超出购物列表范围
print(‘Invalid option!‘)
elif user_choice == "q":
print("--------------- shopping list ---------------")
for o,p in enumerate(shopping_list):
print(o,p)
print("Your current balance:",salary)
exit()
以上是关于购物商城实践的主要内容,如果未能解决你的问题,请参考以下文章
基于vue2.0打造移动商城页面实践 vue实现商城购物车功能 基于VueVuexVue-router实现的购物商城(原生切换动画)效果