作业购物车
Posted 简明现代魔法
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作业购物车相关的知识,希望对你有一定的参考价值。
购物车程序
我的代码
salary = int(input(\'input your salary:\'))
shopping_car = []
shopping_list = [\'iphone\',\'food\', \'book\',\'shirt\']
shopping_price = [5800,20,60,300]
l = shopping_list
p = shopping_price
def show_list():
for i in shopping_list:
print(l.index(i)+1,\'.\',i,\'--------\',p[l.index(i)])
print(\'如果不想买了,输入两次“q”\')
# def shopping():
# show_list()
# y = int(input(\'chose the number:\'))
# try:
# c.append(l[int(y - 1)])
# for i in c:
# s_use = sum(p[l.index(i)])
# if s_use >= salary:
# print(\'too expensive!\')
# break
# else:
# salary = int(salary - s_use)
# print(\'ok,you still have %d\' % salary)
# print(shopping_car)
# except IndexError:
# print(\'can\\\'t find your product.\')
s_less = salary
while 1:
#大循环进入购物流程
show_list()
#展示商品列表
try:
#测试环节用于确认y的值,并且引导错误值退出购物流程
#当错误值被输入,但是用户想继续购物流程,输入除q以外的值就可以继续购物
#注意,x==\'q\'or\'n\'的写法,就相当于x==\'q\'or True
#python中,字符串所表达的布尔值为1,所以,无论输入什么值都会进入if
y = int(input(\'chose the number:\'))
if (y-1)<len(l):
shopping_car.append(l[y - 1])
s_less = s_less - int(p[y-1])
if s_less<0:
print(\'you don\\\'t have enough money!\')
shopping_car.pop()
print(\'you have bought:\',shopping_car)
s_less = salary
continue
else:
print(\'ok,you still have %d\' % s_less)
print(\'and you have bought:\',shopping_car)
else:
print(\'不要乱输!重新来\')
continue
except ValueError:
print(\'pardon me? do you want to quit?\')
x = input(\'press "q" to quit:\')
if x==\'q\':
break
else:
continue
组长代码
思维导图
代码
products = [ \'iPhone7\' ,\'未来简史\' ,\'麦斯威尔咖啡\' ,\'羊角面包\' ,\'特仑苏牛奶\']
prices = [ \'5500\' ,\'30\' ,\'20\' ,\'15\' ,\'10\']
shopping_cart = []
shopping_cart_price = []
while True:
money_left = input( "请输入您本次购物的预算金额:" ) #未限定用户输入整型,须注意后续购物时转换为整型
if money_left == "quit": #此步骤用户亦可输入quit退出;
print("您本次并未购物,byebye!")
exit()
elif money_left.isdigit() == False and money_left != quit: #非数字且非quit,报错重新输入
print("错误请求!只能输入预算金额或quit。")
print("----------------------")
elif int(money_left) > 0 : #数字>0,跳出循环;
money_left = int(money_left)
print("----------------------")
break
else: #数字<=0,报错重新输入;
print("错误请求!只能输入预算金额或quit。")
print("----------------------")
while True:
print("以下为在售商品列表:")
for product_code in range(len(products)): #以列表长度作为循环条件,便于list扩展
print((product_code + 1), products[product_code], prices[product_code]) #编号+1显示,提升体验…
demand = input ( \'购买对应商品请输入编号(quit退出):\')
print("----------------------")
if demand == "quit":
if len(shopping_cart) > 0 :
print("您的购物车内有以下商品:")
for product_buy_code in range(len(shopping_cart)):
print(shopping_cart[product_buy_code] ,shopping_cart_price[product_buy_code])
print("您的余额为:",money_left ,"欢迎再来!")
break
else :
print("您本次并未购物,byebye!")
break
elif demand.isdigit() == False and demand != quit :
print("错误请求!只能输入商品编号或quit。")
print("----------------------")
elif int(demand) -1 >= 0 and int(demand) -1 < len(products) : #之前挖的坑…
demand = int(demand) - 1
if int(prices[demand]) > money_left :
print("余额不足!请选择其他商品,差额:" ,int(prices[demand]) - money_left )
print("您的余额为:", money_left)
print("----------------------")
else :
demand = int(demand)
money_left = money_left - int(prices[demand])
shopping_cart.append( products[demand])
shopping_cart_price.append( prices[demand])
print("购买成功!您的购物车内有以下商品:")
for product_buy_code in range(len(shopping_cart)):
print(shopping_cart[product_buy_code])
print("您的余额为:", money_left )
print("----------------------")
else:
print("错误请求!只能输入商品编号或quit。")
print("----------------------")
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">
以上是关于作业购物车的主要内容,如果未能解决你的问题,请参考以下文章
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段