购物车练习题
在看视屏学习的过程中,有一个课堂练习,该练习结合了 列表 字符串 循环 if等基本内容。
该习题的要求如下:
①启动程序,输入用户的工资:
②之后系统自动打印一个 购物菜单
③然后用户开始选择商品,如果用户手中的钱能买到该商品则加入购物车;否则提示用户您的余额不足,并显示缺的钱
④循环③,直到用户输入q键,结束循环(卡壳在这了)
⑤打印购物车内容(卡壳在这),并且显示剩余内容
思路:很显然,商品和商品的价格都应该是一组数据,这样就使用list对其操作。在列表中存取;我的程序使用两个列表分别存商品与其对应的价格
之后进入循环。购物车也应当是一个列表,在我的设计中购物车列表存放商品和其价格(相邻的两个连续存放)
下面就是,用户选择商品
index=int(input('请输入你想买的物'))-1 #实现选择与确定索引从而在商品和价格列表中确定元素
用户输入1,就是第一个商品(以此类推) ;就建立了一种用户的选择和商品,价格列表索引值之间的一种表达式关系如上。
之后,判断所选商品的价格是否小于余额,大于则舍弃;
if money<price[index] : print('你的余额不足','差:',price[index]-money)
小于则加入购物车(关键是如何加入购物车:在我的程序中把商品名和其价钱加入购物车列表实现该操作)
else: print(goods[index],'已加入到购物车') money-=price[index] select_goods.append(goods[index]) #把选取商品加入购物车列表 select_goods.append(price[index]) #把选取商品的价格加入购物车列表
下面,卡壳的地方是如何跳出循环:(通过某个按键q)
Q=input('是否结束此次购物:') #卡壳 if(Q.lower()=='q'): #判断是否结循环使用break语句 lower() print('结束购物,您已经购买以下商品') break()
其中的lower()是将大小写字母都转换成小写字母。
>>> b'Hello World'.lower() b'hello world'
之后就是打印此时的购物车中的内容,遍历购物车列表即可,关键是如何确定购物车列表的长度:
1 num_of_select=len(select_goods) #求取此时购物车列表的长度 2 x=0 #x用来遍历数组 3 while x<num_of_select: #卡壳在这里,注意while为循环语句,if为条件判断语句 4 print(select_goods[x],select_goods[x+1]) #打印所选商品与价格 5 x+=1
综上,代码如下:
1 #_author_:'alex zhou' 2 # Time: 2018/8/27 0027 3 4 salary=int(input('请输入您的工资')) 5 print('您的工资为:',salary) 6 money=salary 7 8 goods=['iphone','macbook','coffee','pythonbook','bicycle'] 9 price=[5800,9000,32,80,1500] 10 select_goods=[] #新建一个购物车列表用来存入选取的商品 11 info='''**********购物菜单********** 12 1、iPhone 5800 13 2、macbook 9000 14 3、coffee 32 15 4、pythonbook 80 16 5、bicycle 1500 17 ''' 18 print(info) 19 20 while True: #循环购物 21 index=int(input('请输入你想买的物'))-1 #实现选择与确定索引从而在商品和价格列表中确定元素 22 if money<price[index] : 23 print('你的余额不足','差:',price[index]-money) 24 else: 25 print(goods[index],'已加入到购物车') 26 money-=price[index] 27 select_goods.append(goods[index]) #把选取商品加入购物车列表 28 select_goods.append(price[index]) #把选取商品的价格加入购物车列表 29 30 Q=input('是否结束此次购物:') #卡壳 31 if(Q.lower()=='q'): #判断是否结循环使用break语句 lower() 32 print('结束购物,您已经购买以下商品') 33 num_of_select=len(select_goods) #求取此时购物车列表的长度 34 x=0 #x用来遍历数组 35 while x<num_of_select: #卡壳在这里,注意while为循环语句,if为条件判断语句 36 print(select_goods[x],select_goods[x+1]) #打印所选商品与价格 37 x+=2 38 print('您的余额为:', money) 39 print('欢迎下次光临') 40 break
#视屏的做法下次添加
自己代码需要改进的地方有 (1)建立的两个列表分别存放商品与其对应的价格,有很大的弊端 当数据无限多的时候 会存在数据存储的错乱性
改进方法:可以通过一个嵌套列表解决问题
(2)购物车列表里存放商品和价格太繁琐
改进方法:存放选择商品的索引值
(3)没有判断输入的薪水是否为整型,也没有判断选择的商品是否存在
(4)逻辑上存在混乱,编程时一定要注意逻辑性 if else ;先从整体的大方向上确定逻辑,再进行细节的书写
同时,需要注意两个函数:
bytearray.isdigit() #判断字符是否为数字字符串
Return true if all bytes in the sequence are ASCII decimal digits and the sequence is not empty, false otherwise.
ASCII decimal digits are those byte values in the sequence b'0123456789'.
>>> '123'.isdigit()
True
>>> '123w'.isdigit()
False
enumerate(iterable, start=0) #枚举函数,返回一个元组包含index和元素
Return an enumerate object. iterable must be a sequence, an iterator,
or some other object which supports iteration.
The __next__() method of the iterator returned by enumerate()
returns a tuple containing a count (from start which defaults to 0)
and the values obtained from iterating over iterable.
#返回一个枚举类型,迭代式必须是一个序列,一个迭代式或者支持迭代的其它对象。
#返回一个包含计数的元组(默认从0开始计数)并且从不断迭代所获得的值
例如:
1 >>> a=['wuchao','jinxing','xiaohu','sanpang','eer','zhou','zhou'] 2 >>> b=enumerate(a,1) #生成一个元组 3 >>> for x in b: #遍历整个元组 4 print (x) #打印 5 6 (1, 'wuchao') 7 (2, 'jinxing') 8 (3, 'xiaohu') 9 (4, 'sanpang') 10 (5, 'eer') 11 (6, 'zhou') 12 (7, 'zhou')
改进的代码如下:
1 #_author_:'alex zhou' 2 # Time: 2018/8/28 3 #第一种方法,建立两个列表分别存商品和价格(也是我的思路) 4 #存在的问题,商品和价格未知必须一一对应,不好 5 ''' 6 name1=['mac','book','bike','kindle'] 7 price=[9000,50,2000,600] 8 ''' 9 10 #第二种方法:列表的嵌套,[[s商品,价格],[商品2,价格2].....] 11 product_list=[ #注意此列表的定义,列表中包含元组tuple,嵌套 12 ('Mac',9000) , 13 ('kindle',800), 14 ('tesla',900000), 15 ('pythonbook',105), 16 ('bike',2000), 17 ] 18 shopping_car=[] #购物车空列表 19 saving=input('please input your saving:') #输入余额,此时saving是个字符串 20 if saving.isdigit(): #判断用户输入的是否为整型 21 saving=int(saving) 22 while True: 23 #for i in 可迭代对象(序列): 24 # 操作 25 #for i in product_list: #方法一:遍历菜单,打印购物菜单,注意此种方法 26 #print(product_list.index(i),i) #求元素在列表中的索引值 27 28 for i,v in enumerate (product_list,1): #方法二:enumerate();i v返回两个值,其中i包含一个索引,v为一个元组 29 print(i,'>>>',v) #a='333' 30 #a,b=[2,3] #用两个变量接受元组,把变量分别拿出来 31 #print(a,b) 32 33 choice=input('请选择购买商品编号[退出:q]:') #选购商品 34 if choice.isdigit(): #是否输入的为整型,验证输入是否合法 35 choice=int(choice) 36 if choice>0 and choice<len(product_list): #代码不要写死 if choice>0 and choice<6 这种就很死# #判断选购的商品是否存在 37 p_item=product_list[choice-1] #把商品和其价格的元组取出来,注意对应关系 -1 38 if p_item[1]<=saving: #如果价钱够,用本金减去改该商品价格,并将商品加入购物车 39 saving-=p_item[1] 40 shopping_car.append(p_item[0]) #把商品加入购物车 41 else: 42 print('您的余额不足,还剩%s'%saving) 43 else: 44 print('编码不存在') 45 46 elif choice=='q': #结束购物,打印购物车中的内容 47 print('***********您已经购买如下商品************') 48 #循环遍历购物车里的内容,购物车存放的是已买商品 49 for x in shopping_car: #打印购物车中内容,遍历购物车数组 50 print (x) 51 print('您还剩%s元'%saving) #字符串的格式化 52 break #exit()结束所有程序 53 else: 54 print('invalid input') 55 else: 56 print('您输入的钱数有错误')
浙公网安备 33010602011771号