第二周练习 part1 --- 简易版购物车程序

#购物车程序

 1 '''启动程序后让用户输入工资,然后打印商品列表
 2 用户根据编号选择购买的商品
 3 用户的钱足够就直接扣钱,不够就提醒
 4 可随时退出,退出时打印已购买商品和余额'''
 5 
 6 salary=input("请输入你本月的工资: ")
 7 goods=['1.可乐(12)','2.雪碧(13)','3.Notebook(5)','4.iPhone(8888)','5.Earphone(3643)']
 8 price=[12, 13, 5, 8888, 3643]
 9 cost=0
10 bought = []
11 startshop = 1
12 k = 0
13 print(goods)
14 while startshop == 1:
15     i = input('Please type the goods number of what you want to buy, when you are done, type "0" to quit: ')
16     if int(i) == 0:
17         startshop = 0
18     else:
19         if price[int(i) - 1] + cost > int(salary):
20             print("You don't have enough money.")
21             j = input('Do you want to buy other goods? Type 1 for yes, type 0 for quit: ')
22             if int(j) == 1:
23                 continue
24             if int(j) == 0:
25                 startshop = 0
26         else:
27             cost = cost + price[int(i) - 1]
28             bought.insert(k,goods[int(i)-1])
29             k+= 1
30 print("You have bought ", bought[:], " and cost ", cost, " yuan.", "Now you have", int(salary) - cost, "yuan left")

-------------------------------------------------New Version---------------------------------------------------

 

 1 salary=input("Please input your salary: ")
 2 if salary.isdigit():
 3     salary = int(salary)
 4     goods=['Coco(12 yuan)','Sprite(13 yuan)','Notebook(5 yuan)','iPhone(8888 yuan)','Earphone(3643 yuan)']
 5     price=[12, 13, 5, 8888, 3643]
 6     cost=0
 7     bought = []
 8     startshop = 1
 9     k = 0
10     for a in enumerate(goods): print(a)
11     while True:
12         i = input('Please type the goods number of what you want to buy, when you are done, type "q" to quit>>> ')
13         if i.isdigit():
14             i = int(i)
15             if i > 4 or i < 0:
16                 print('Pleasure type the number shown before the merchandise.')
17                 continue
18             if price[i] + cost > int(salary):
19                 print("You don't have enough money.")
20                 while True:
21                     j = input('Do you want to buy other goods? Type y for yes, type q for quit: ')
22                     if j == 'y':
23                         break
24                     elif j == 'q':
25                         print("You have bought")
26                         for w in bought:
27                             print(w)
28                         print("and cost", cost, "yuan.", "Now you have", salary - cost, "yuan left.")
29                         exit()
30                     else:
31                         print('33[41;1mPleasure type "y" or "q"!33[0m')
32                         continue
33             else:
34                 cost = cost + price[i]
35                 bought.insert(k, goods[i])
36                 k += 1
37                 print('Added', goods[i], 'to your list successfully. Now you have', salary - cost, 'yuan left.')
38         elif i == 'q':
39             print("You have bought")
40             for w in bought:
41                 print(w)
42             print("and cost", cost, "yuan.")
43             print("Now you have", salary - cost, "yuan left.")
44             exit()

金沙官网线上, 

 

本文由金沙官网线上发布于编程,转载请注明出处:第二周练习 part1 --- 简易版购物车程序

您可能还会对下面的文章感兴趣: