python練習(一):購物車

1、要求

1.啓動程序後,讓用戶輸入工資,而後打印商品列表app

2.容許用戶根據商品編號購買商品spa

3.用戶選擇商品後,檢測餘額是否足夠,夠就直接扣款,不夠就提醒code

4.可隨時退出,退出時打印已購買商品和餘額blog

2、思路

 

 

3、代碼

 1 commodity = [[1,'滿漢全席',5000],[2,'燒烤',500],[3,'自助餐',50]]
 2 shopping_cards = []
 3 flag = 0
 4 while flag==0:
 5     salary = int(input("請輸入你的工資(若想退出請輸入0):"))
 6     if salary==0:
 7         flag==1
 8         break
 9     info = '''
10     ------ 商品信息 ------
11     編號    物品    價格
12     '''
13     print(info)
14     for i in commodity:
15         print(i)
16     while(salary!=0 & flag!=1):
17         a = int(input("請輸入你想買的商品編號(退出請輸入0):"))
18         if a==0:
19             flag = 1
20             break
21         for x in commodity:
22             if a==x[0] & salary>=x[2]:
23                 shopping_cards.append(x)
24                 salary = salary - x[2]
25                 break
26             elif a==x[0] & salary<x[2]:
27                 print("你的工資不足以支付,請合理消費!")
28                 break
29         if salary<commodity[2][2]:
30             print("你的餘額不足以購買任意商品了!")
31 print("------ 已購商品 ------")
32 for y in shopping_cards:
33     if shopping_cards.count(shopping_cards)==0:
34         print('NULL')
35         break
36     print(y)
相關文章
相關標籤/搜索