购物车代码(学习版,很多地方需要优化)

购物车练习

现有知识编写的代码,还有很多地方需要优化。先记录一下!

salary = int(input("salary is :"))
msc = '''
---------------shopping list-------------
1. iphone12      6800
2. mac book      9000
3. bubble        18
4. python book   60
5. bicyle        700
-----------------end---------------------
'''
print(msc)
shopping_list = ["1",'iphone12s',6800,"2","mac book",9000,"3","coffee",32,"4","python book",80,"5","bicyle",1500]
#number = input(">>>")
#print(shopping_list.index(number))
count = 0
LIST = []
while True:
    number = input(">>>:")
    if  number == 'quit':
        break
    goods = shopping_list[shopping_list.index(number)+1]
    price = shopping_list[shopping_list.index(number)+2]
    if  salary < price:
        print("余额不足, "+'-'+str(price-salary))
    else:
        print("已加入"+goods+"到你的购物车,"+"当前余额:"+str(salary-price))
        salary = salary-price
        LIST.append(goods+' '+str(price))
        count += 1
    continue
print("您已购买以下商品")
for i in range(count):
    print(LIST[i])
else:
    print("您的余额为:"+str(salary))
    print("欢迎下次光临" )
上一篇:Python学习之关于方法和类


下一篇:python列表练习