第二週做業


購物車實現功能並函數封裝,
購物車
1. 註冊
2. 登錄
3. 購物
4. 支付
5. 充值
退出

用戶表格記錄,用戶名:密碼:餘額 user.txt
msj:123456:10000
swb:123:911900
lyh:123:1000
scg:321:3000

記錄購物車信息,用戶名:商品名:價格:數量git

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

函數代碼:app

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內存放的信息。spa

相關文章
相關標籤/搜索