第二周作业


购物车实现功能并函数封装,
购物车
1. 注册
2. 登陆
3. 购物
4. 支付
5. 充值
退出

用户表格记录,用户名:密码:余额 user.txt
msj:123456:10000
swb:123:911900
lyh:123:1000
scg:321:3000

记录购物车信息,用户名:商品名:价格:数量

msj:apple:10:1
msj:tesla:10:1
msj:apple:1000:2
scg:mac:3000:2
scg:chicken:10:10

函数代码:

1 def getname():
2     '''
3     得到用户输入的用户名,交由登录和注册使用
4     :return: 返回整理后用户
5     '''
6     name = input('请输入用户名:').strip()
7     return name
def getpwd():
    '''
    获得用户输入的密码,交由登录和注册使用
    :return: 整理过的密码
    '''
    pwd = input('请输入密码:').strip()
    return pwd
def is_db_get_u(name):
    """
    从数据文本下的获取用户名,判断键入用户名是否在user.txt
    :return: 如果在,返回True,如果不在返回False
    """
    with open(r'user.txt',mode='r',encoding='utf-8') as rf:
        for line in rf:
            u = line.strip('\n').split(':')[0]
            if name == u:
                return True
        else:return False
 1 def is_pwd_corret(name,pwd):
 2     '''
 3     检测对应的用户名和密码是否正确
 4     :param name:
 5     :param pwd:
 6     :return: 正确的话返回True,否则返回False
 7     '''
 8     with open(r'user.txt',mode='r',encoding='utf-8')as rf:
 9         for line in rf:
10             u,p,*_=line.strip('\n').split(':')
11             if name ==u and pwd ==p:
12                 return True
13         else:return False
 1 def register():
 2     '''
 3     注册,检测用户名是否存在,
 4     存在,打印用户名存在,不存在,将用户名:密码:0
 5     :return:
 6     '''
 7     name=getname()
 8     pwd = getpwd()
 9     if is_db_get_u(name):
10         print('用户已存在')
11     else:
12         msg = "%s:%s:0\n"%(name,pwd)
13         with open(r'user.txt',mode='a',encoding='utf-8') as wf:
14             wf.write(msg)
15         print('注册完成')
 1 def login():
 2     '''
 3     登录
 4     :return:登录成功返回name,登陆失败返回None
 5     '''
 6     name = getname()
 7     pwd = getpwd()
 8     if is_pwd_corret(name,pwd):
 9         print('登陆成功')
10         return name
11     else:
12         print('用户名或密码错误,请重新输入')
13         return None
 1 def shopping(db_name):
 2     '''
 3     同展示内容,让用户输入自己想要的东西 和数量,并将数据添加到购物车shopping.txt当中
 4     :param db_name:用户名
 5     :return:None
 6     '''
 7     msg_dic = {
 8         'apple': 1000,
 9         'tesla': 100000,
10         'mac': 3000,
11         'lenovo': 30000,
12         'chicken': 10,
13     }
14     while True:
15         for key, price in msg_dic.items():
16             print('商品名:%s  价格:%s' % (key, price))
17         choice = input('请输入想要的商品名:').strip()
18         if not choice or choice not in msg_dic:
19             print('请重新输入商品名')
20             continue
21         while True:
22             count = input('请输入购买的数量:').strip()
23             if not count.isdigit():
24                 continue
25             else:
26                 msg = '%s:%s:%s:%s\n' % (db_name, choice, msg_dic[choice], count)
27                 with open(r'shoppingcart.txt', mode='a', encoding='utf-8') as f:
28                     f.write(msg)
29                 break
30         break
 1 def alter_money(db_name,money):
 2     """
 3     用于修改账户余额的功能,提供给充值和支付功能使用,根据用户找到相应的账户信息,并增加或减少相应的金额,并打印信息
 4     :param db_name: str 来自login 确定账户信息
 5     :param money: int 支付和充值信息,正负由支付功能和充值功能确定,还需要检测钱是不是整形
 6     :return:
 7     """
 8     with open(r'user.txt',mode='r',encoding='utf-8')as f,\
 9             open(r'user.txt.swap',mode='w',encoding='utf-8')as wf:
10         for line in f:
11             n,p,m=line.strip('\n').split(':')
12             m=int(m)
13             if db_name == n:
14                 line='%s:%s:%s\n'%(n,p,m+int(money))
15             wf.write(line)
16     os.remove('user.txt')
17     os.rename('user.txt.swap','user.txt')
 1 def total_price(db_name):
 2     '''
 3     用于计算购物车,某位用户购买货物的总价值,用于支付函数payment使用
 4     :param db_name: 用户名
 5     :return: 总价 int 类型
 6     '''
 7     tprice = 0
 8     with open(r'shoppingcart.txt',mode='r',encoding='utf-8') as f:
 9         for line in f:
10             info = line.strip('\n').split(':')
11             if  info:
12                if db_name == info[0]:
13                    tprice += int(info[2])*int(info[3])
14     return tprice
 1 def get_db_money(db_name):
 2     '''
 3     根据用户名获得账户的余额,交由支付payment使用
 4     :param db_name: 用户名
 5     :return: 余额   int类型
 6     '''
 7     with open(r'user.txt',mode='r',encoding='utf-8') as f:
 8         for line in f:
 9             u = line.strip('\n').split(':')[0]
10             if db_name == u:
11                 return int(line.strip('\n').split(':')[2])
 1 def clear_cart(db_name):
 2     '''
 3     用户支付后,清空shoppingcart的购物信息,交支付函数payment函数使用,会清空购车对应用户名的每行信息
 4     :param db_name: 用户
 5     :return:
 6     '''
 7     with open(r'shoppingcart.txt',mode='r',encoding='utf-8') as f,\
 8             open(r'shoppingcart.txt.swap',mode='w',encoding='utf-8') as wf:
 9         for line in f:
10             u = line.strip('\n').split(':')[0]
11             if db_name == u:
12                 line =''
13             wf.write(line)
14     os.remove('shoppingcart.txt')
15     os.rename('shoppingcart.txt.swap','shoppingcart.txt')
 1 def payment(db_name):
 2     """
 3     通过购物车文件内相同用户名的计算物品价格,在通过用户名检查user.txt下的第三个值为账户余额,
 4     判断余额是否大于等于购物车合计,如果大于等于提示支付,并修改账户内余额,否则提示余额不足请充值
 5     :param db_name:用户名,需要来自login
 6     :return:
 7     """
 8     if total_price(db_name)>get_db_money(db_name):
 9         print('账户余额不足,请充值')
10     else:
11         alter_money(db_name,-total_price(db_name))
12         clear_cart(db_name)
13         print('支付成功')
 1 def recharge(db_name):
 2     '''
 3     充值功能,获得金钱数额,输错重新再输,正确就调用alter_money方法填写进数据表格中
 4     :param db_name:
 5     :return:
 6     '''
 7     while True:
 8         money = input('请输入充值的金额:').strip()
 9         if money.isdigit() and int(money)>0:
10             alter_money(db_name,money)
11             break
12         print('请重新输入金额')

主代码

 1 db_name = None
 2 while True:
 3     print("""
 4     0. 退出
 5     1. 登录
 6     2. 注册
 7     3. 购物
 8     4. 支付
 9     5. 充值
10     """)
11     choice = input('请输入你想要的操作:').strip()
12     if choice =='0':#退出
13         print('退出成功')
14         break
15     elif choice =='1':#d登录
16         db_name = login()
17     elif choice =='2':#注册
18         register()
19     elif choice =='3':#购物
20         if db_name:
21             shopping(db_name)
22         else:
23             print('请登录')
24     elif choice =='4':#支付
25         if db_name:
26             payment(db_name)
27         else:
28             print('请登录')
29     elif choice=='5':#充值
30         if db_name:
31             recharge(db_name)
32         else:
33             print('请登录')
34     else:
35         print('请重新输入想要的操作')

上述函数还涉及使用了os模块,用于修改txt内存放的信息。

 

 
posted @ 2018-09-23 11:44  msjaxuexi  阅读(113)  评论(0编辑  收藏  举报