程序练习 

请闭眼写出以下程序。

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额

闭眼写版本:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:Zoe
 4 '''
 5 程序:购物车程序
 6 
 7 需求:
 8 
 9 启动程序后,让用户输入工资,然后打印商品列表
10 允许用户根据商品编号购买商品
11 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
12 可随时退出,退出时,打印已购买商品和余额
13 '''
14 '''
15 伪代码:
16 
17 脚本直接写入商品列表,以课程要求以列表方式存储(实际字典形式更加),存储商品列表;(优化版本需要预先写入商品信息到文件,打开文件,读取文件信息)
18 
19 用户输入salary:int(input('please input your salary'));
20 判断用户输入的salary信息是否为正整数或浮点数(具体参考设计需要);
21     如果True:
22         存储成功,输出"你的购物车余额为%d"%salary
23     else:
24         输出“invalid salary"
25 询问用户是否进行商城购物,是:
26     打印商品列表:一种是循环打印,一种是直接全部打印;
27 else:
28     退出并打印余额,欢迎再来。
29 输出"用户是否需要购买商品":
30     if True:
31         让用户输入需要购买的商品id
32             判断用户余额是否大于需要购买的商品id的价格,如果True:
33                 print("you have add this item %s into your shopping cart, your current balance is %d")%(商品名称,余额)
34             else:
35                 print('sorry,you don't have enough balance to buy it.')
36     else:如果输入“quit”
37         退出购买进程。
38         print(购物车商品列表和余额)
39 '''
40 
41 goods=[[1,"mac book pro",20000],[2,'iwatch',3000],[3,'pen',300],[4,'huawei',4000],[5,'tree',100],[6,'mirror',39.9],[7,'football',99.9]]
42 balance=input('please input your salary as your account balance')
43 if balance.isdigit():
44     print('your account balance is %s'%balance)
45     balance=float(balance)
46 else:
47     print('invalid input,please input it again')
48 
49 choose_shopping=input('if you want to go to shopping,please input \'Y\',if you want to exit,please input \'q\' ')
50 if choose_shopping=='Y'or choose_shopping=='y':
51     for i in goods:
52         print(goods)
53         choose_items=input('input goods\' No. can print good\'s name and price')
54         if choose_items.isdigit:
55             choose_items=int(choose_items)
56             print(goods[choose_items-1][1],goods[choose_items-1][2])
57             check_purchase=input('please input "y" to confirm add {item} to your shopping cart'.format(item=goods[choose_items-1][1]))
58             if check_purchase=='y' or check_purchase=='Y':
59                 if balance-goods[choose_items-1][2]>=0:
60                     print("you have add {item} into your shopping cart,and your current balance is {yue}".format(item=goods[choose_items-1][1],yue=balance-goods[choose_items-1][2]))
61                 else:
62                     print('sorry,you don\'t have enough balance to buy it.')
63             else:
64                 print('sorry,you have not confirm the deal')
65         else:
66             print('invalid input!please confirm it again')
67     else:
68         pass
69 elif choose_shopping=='Q'or choose_shopping=='q':
70     quit()
71 else:
72     print('invalid input,we don\'t know what you want to do')

——有点,基本上判断和循环有了,但是退出实时退出有问题,另外循环判断都相应的有点欠缺。

暂时不改了。

 

Alex版本:

 

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:Zoe
 4 
 5 #定义商品列表
 6 product_list=[
 7     ('Iphone',5800),
 8     ('Mac Book Pro',9800),
 9     ('Bike',800),
10     ('Iwatch',3000),
11     ('Coffee',31),
12     ('Pen',100)]
13 #定义一个空列表存放购物车数据
14 shopping_list=[]
15 salary=input('Input your salary:')
16 #防止int强转字符串报错,出现错误;不对input的数值进行int;通过if判断后进行转换。
17 if salary.isdigit():
18     salary=int(salary)
19     #打印商品列表
20     while True:
21         '''#通过下标找出产品编号
22         for item in product_list:  #通过下标找出产品编号
23             print(product_list.index(item),item)'''
24         #通过enumerate()函数取得列表的下标
25         for index,item in enumerate(product_list):
26             print(index,item)
27         user_choice=input('选择要买吗?>>>')
28         if user_choice.isdigit():  #注意:str.isdigit()一定要加上(),才会返回True或False。否则返回的是内存对象,会导致下面的判断出错。能够运行的原因在于if后面的值不是非零,非空值都会默认为True。
29             user_choice = int(user_choice)
30             if user_choice>=0 and user_choice<len(product_list):
31                 p_item=product_list[user_choice]
32                 if p_item[1]<=salary:
33                     shopping_list.append(p_item)
34                     salary-=p_item[1]
35                     print('Added %s into shopping cart. Your current balance is \033[31;m%s\033[0m'%(p_item,salary))  #高亮显示背景色和前景色,见随笔
36                 else:
37                     print('\033[41;1mYour current balance is %s\033[0m'%(salary))
38             else:
39                 print('product code %s is not exist!'%(user_choice))
40         elif user_choice == 'q':
41             print('shopping list'.center(100,'-')) #格式化输出
42             for p in shopping_list:
43                 print(p)
44             print('Your current balance:',salary)
45             exit()
46         else:
47             print('invalid option')

 

优化版本:

 

posted on 2017-06-13 17:28  Zoe233  阅读(225)  评论(0编辑  收藏  举报