Python小练习008

简易购物程序的实现。(一种方式)

 1 # 简易购物程序
 2 
 3 money = 20000
 4 goods_list = ['IPhone 8', 'Bicycle', 'Banana', 'Xiaomi Note', 'MacBook', '矿泉水']
 5 goods_price = [9000, 800, 20, 3200, 12000, 5]
 6 print("欢迎来到智能购物中心!您卡上余额共有%d元。"% money)
 7 print()
 8 print('商品信息:')
 9 count = 0
10 while count < len(goods_list):
11     print('商品编号:%d' % count + "   " + goods_list[count] + '----' +  str(goods_price[count]) + '',end="\n")
12     count += 1
13 print()
14 flag = True
15 buy_lists = []
16 buy_price = []
17 while flag:
18     print()
19     num = input("请输入要购买商品的编号:")
20     print()
21     if num.isdigit() and int(num) < len(goods_list):
22         num = int(num)
23         if num < len(goods_list) and money >= goods_price[num]:
24             money -= int(goods_price[num])
25             print("您购买了%s" % goods_list[num], '剩余%d元' % money)
26             buy_lists.append(goods_list[num])
27             buy_price.append(goods_price[num])
28         else:
29             print("余额不足!")
30             flag = False
31     else:
32         print("请输入正确的商品编号!")
33 print()
34 for m in range(len(buy_lists)):
35     buy_lists[m] +=  '--' + str(buy_price[m]) + ''
36 print("您共购买了%d件商品:%s" % (len(buy_lists),' , '.join(buy_lists)) + '\n' + '您卡上剩余%d元' %(money) + '\n' + '欢迎下次光临!')

 

运行结果:

 简易购物程序的实现。(二种方式)

 1 goods_lists = [
 2     ('IPhone 8', 9000),
 3     ('Bicycle', 800),
 4     ('Banana', 20),
 5     ('Xiaomi Note', 3200),
 6     ('MacBook', 12000),
 7     ('矿泉水', 5),
 8 ]
 9 money = input('Please input your money:')
10 shopping_list = []
11 if money.isdigit():
12     money = int(money)
13     while True:
14         for m,n in enumerate(goods_lists, 1):
15             print(m,'>>',n)
16         choice = input('选择购买商品编号【输入Q结束购买】:')
17         if choice.isdigit():
18             choice = int(choice)
19             if choice > 0 and choice < len(goods_lists):
20                 goods_item = goods_lists[choice-1]
21                 if goods_item[1] < money:
22                     money -= goods_item[1]
23                     shopping_list.append(goods_item)
24                 else:
25                     print('余额不足!还剩%s元' %money)
26             else:
27                 print('请输入正确的商品编号!')
28         elif choice == 'Q' or 'q':
29             print('您已购买如下商品:')
30             for i in shopping_list:
31                 print(i)
32             print('您还剩%s元'%money)
33             break
34         else:
35             print('输入错误,请重新输入!')

运行结果:

目前能力不足,以上两种方式都有很多不足之处,继续努力!

posted @ 2018-03-29 19:48  Jysu  阅读(128)  评论(0编辑  收藏  举报