初学python(2): 简单购物车的制作
作用:1.实现购物的流程(陈列商品,显示价格,加入购物车)
2.最终将购物车的内容及用户信息保存下来,下次登录时可以调用
该小程序包括程序主体,user.txt, goods.txt 以及 以用户名命名的 txt 文件用来保存购买数据。
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # editor:skQuat 4 5 username = input("enter your username:") 6 user_in = False 7 users= [] 8 shoppinglist = [] 9 shoppingcart = [] 10 11 #读取用户记录,如果是新用户就注册 12 with open('user.txt', 'r+') as user_name: 13 for x in user_name.readlines(): 14 users.append( x.split() ) 15 if len( x ) > 1 and username == x.split()[0]: 16 user_in = True 17 money = int(x.split()[1]) 18 print("Avaliable money is %d" % (money)) 19 with open('%s.txt' % username , 'r') as goods: 20 for x in goods: 21 shoppingcart.append( x.split() ) 22 print("------shopping cart-------") 23 for i , x in enumerate(shoppingcart): 24 print(i + 1 , x[0] , x[1]) 25 26 if user_in: 27 pass 28 else: 29 money = input("enter your money:") 30 if money.isdigit(): 31 money = int(money) 32 else: 33 print("money error") 34 user_name.write('\n' + username + ' ' + str(money)) 35 users.append([username, money]) 36 37 #读取商品数据 38 with open('goods.txt', 'r+') as goods: 39 for x in goods.readlines(): 40 if len(x) > 1: 41 shoppinglist.append(x.split()) 42 43 #购买过程 44 while True: 45 #打印商品 46 print("-------shopping list--------") 47 for i, x in enumerate(shoppinglist): 48 print( i + 1, x[0], x[1]) 49 50 #输入想要什么 51 number = input( ">>>:" ) 52 53 if number.isdigit() and int(number) > 0 and int (number) < len(shoppinglist): 54 number = int( number ) 55 #购买流程 56 if money > int( shoppinglist[number - 1][1] ) : 57 print("%s has added to you shopping cart, you avaliable monry is %d" % (shoppinglist[number - 1][0] , money)) 58 money = money - int( shoppinglist[number - 1][1] ) 59 shoppingcart.append(shoppinglist[number - 1]) 60 shoppinglist.pop(number - 1) 61 else: 62 print( "you money is not enough" ) 63 elif number == 'q': 64 print("exit...........") 65 break 66 else: 67 print("no such produce as the number") 68 69 print("------shopping cart-------") 70 for i, x in enumerate(shoppingcart): 71 print( i + 1, x[0], x[1]) 72 print("you balance is", money ) 73 74 ##输出,商家端可以直接全部重新输入一次,清掉之前的内容达到更新效果,买家仍需考虑 75 76 with open( 'goods.txt', 'w') as goods: 77 for x in shoppinglist: 78 goods.write( x[0] + ' ' + x[1] + '\n') 79 80 with open( '%s.txt' % username, 'w') as goods: 81 for x in shoppingcart: 82 goods.write( x[0] + ' ' + x[1] + '\n') 83 84 for x in users: 85 if x[0] == username: 86 x[1] = money 87 88 with open( 'user.txt', 'w') as user: 89 for x in users: 90 user.write( x[0] + ' ' + str( x[1] ) + '\n')
user.txt
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
jack 1920
alex 10000
goods.txt
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
iphone 8000 psv 1000 bicycle 300
jack.txt
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
book 50 coffee 30 ps4pro 3000
alex.txt(空)
卖家版可以使用同样的原理,通过调用 goods.txt 进行商品读取,写入一个列表中,在列表中进行增减,然后将列表写入,覆盖掉之前的内容。
缺点: 每次调用都要对一个文档进行全部的读写,缺乏效率。同时操作重复, 可以用函数简化。
####经验:对一个 txt 文件进行读写时,可以用 %s 代替文件名,用变量输入,达到根据需求创建新 txt 的作用。