改进的购物车程序,能实现更完善的功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了改进的购物车程序,能实现更完善的功能相关的知识,希望对你有一定的参考价值。
用户段:
# -*- coding:utf-8 -*-
shop_line={}
info_user={}
shop_car=[]
import re
for line in open(‘C:\Users\Administrator.SKY-20140501YNV\Desktop\sshop.txt‘):
line=line.rstrip(‘\n‘)
key=re.split(":",line)
shop_line[key[0]]=key[1]
for line in open(‘C:\Users\Administrator.SKY-20140501YNV\Desktop\info.txt‘):
line=line.rstrip(‘\n‘)
key=line.split(":")
info_user[key[0]]=key[1]
user_name=raw_input("请用户输入用户名》")
if user_name in info_user:
salary=info_user[user_name]
salary=int(salary)
else:
while True:
salary=raw_input("请输入您的工资:")
if salary.isdigit():
salary=int(salary)
lines=‘:‘.join([user_name,str(salary)])
open(‘C:\Users\Administrator.SKY-20140501YNV\Desktop\info.txt‘,‘a‘).write(‘\n‘)
open(‘C:\Users\Administrator.SKY-20140501YNV\Desktop\info.txt‘, ‘a‘).write(lines)
break
while True:
for i in shop_line:
print i,shop_line[i]
chioce=raw_input("请用户输入选择的商品,若想退出,请输入q》")
if chioce==‘q‘:
print shop_car,salary
lines=open(‘C:\Users\Administrator.SKY-20140501YNV\Desktop\info.txt‘,‘r‘).readlines()
flen=len(lines)
for i in range(flen):
m=re.match(user_name,lines[i])
if m is not None:
lines[i]=‘:‘.join([user_name,str(salary)])
open(‘C:\Users\Administrator.SKY-20140501YNV\Desktop\info.txt‘,‘w‘).writelines(lines)
break
elif chioce in shop_line and salary-int(shop_line[chioce])>=0:
shop_car.append(chioce)
salary-=int(shop_line[chioce])
以上是关于改进的购物车程序,能实现更完善的功能的主要内容,如果未能解决你的问题,请参考以下文章