最近在學習Python,便利用業餘時間開發了一個模擬ATM搶銀行的小程序,不廢話,直接上程序
#!/usr/bin/env python #coding=utf-8 # Name: AtmCard.py __author__ = 'kumikoda' import pickle import sys import time import hashlib import os def main(): '''打印功能信息列表,並提供用戶輸入接口再進行具體項目操作''' cardNum = login('id') if cardNum == False: sys.exit() while True: wecome = raw_input("""歡迎使用本銀行信用卡中心! 請輸入如下功能對應的數字. 功能如下: 1. 第一次使用卡,請對卡進行初始化操作(重要). 2. AMT現金取款 3. 購物、消費等刷卡消費 4. 信用卡還款 5. 退出 Please input(1/2/3/4/5):""").split() if len(wecome) == 0: continue number = wecome[0] if number == '1': init() continue elif number == '2': draw_money(cardNum) elif number == '3': buy(cardNum) elif number == '4': repayment(cardNum) elif number == '5': print "Exit, Good Bye!" sys.exit() else: print "你輸入的數字不在功能列表內,請重新輸入." continue def login(number=None): '''用戶登錄驗證''' with open('F:\Python\Balance_tab','rb') as f: # 讀取文件並將數據放入元組 for line in f: keys = line.split()[1] print keys vlues = line.split()[2] print vlues con = 0 while True: CardNum = raw_input("\033[32;1m請輸入您的信用卡卡號:") if CardNum == keys: UserPwd = raw_input("\033[32;1m請輸入你的登錄密碼:") md5 = hashlib.md5() md5.update(UserPwd) UserPwd = md5.hexdigest() print md5.hexdigest() if UserPwd == vlues: print "登錄成功." if number == "id": return CardNum else: return True else: con = con + 1 print "輸入的密碼或者用戶名有誤,請重新輸入,[Error %s]" % con if con == 3: print "輸入的密碼錯誤三次." return False else: continue else: print "沒有找到你輸入的卡號,請重新輸入." continue def init(): '''新卡初始化(1.額度加載 2.設置修改卡密碼 3. 添加記賬日誌格式頭部)''' money = [] with open('F:\Python\Balance_tab','rb') as f: m = f.read().split()[-1] money.append(m) temp_log(Tlist=money, content='dump') print "Temp money load OK." initDlist = ['賬號' + '時間' + '\t', '摘要' + '\t', '消費金額' + '\t', '手續費' + '\n'] day_log(initDlist) print "Day log write OK." def draw_money(cardNum): '''取現功能''' while True: input_money = raw_input("歡迎使用本中心自助取款ATM系統,請輸入本次取款金額:") if len(input_money.split()) == 0: continue userinfo = raw_input("取現需收取%5的手續費,確認是否取現[Y/n]:") if userinfo == "Y" or userinfo == "y": ## load總金額,並計算輸出 t1 = temp_log(content='load') money = float(t1[0]) fee = float(input_money) * 0.05 # 手續費 draw = float(input_money) + fee # 本次取款額+手續費 if draw >= money: print "賬戶餘額不足,請重新輸入取款金額." continue else: total = money - draw # 賬戶餘額 - 本次取款額 print "您本次取現金額爲: RMB% s 賬戶可取現總金額爲: RMB% s" %(input_money, total) ## dump 消費後總金額,到文件內保存 totals = [] totals.append(total) temp_log(Tlist=totals, content='dump') ## 寫入日誌文件 drawtime = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) # 獲取當前日期 Dlist = [cardNum + '\t', drawtime + '\t', 'draw money' + '\t', '-' + input_money + '\t', '-' + str(fee) + '\n'] day_log(Dlist) scont = raw_input("您是否繼續取現:[Y/n]") if scont == "Y" or scont == "y": continue elif scont == "N" or scont == "n": print "Exit!" break elif userinfo == "N" or userinfo == "n": print "Exit!" sys.exit() else: print "你輸入[Y/n]按回車鍵確認:" continue def buy(cardId): '''購物/消費等''' user_Money = int(temp_log(content='load')[0]) while True: list = {1: ['Iphone 6s', '5000'], 2: ['MacBook', '15000'], 3: ['Tea', '550'], 4: ['Vegetables', '450'], 5: ['coffee', '250']} print "購物清單:" for x, y in list.items(): z = ' '.join(y) print x, z yn_list = raw_input("Do you need to buy [Y/n] or add shoping list [S] Enter:") if yn_list == "Y" or yn_list == "y": user_input = raw_input("請輸入你要購買的商品名稱對應的編號:") k = int(user_input) if len(user_input) == 0: print "你沒有輸入任何商品的名稱,請重新輸入." continue amount = int(list[k][1]) # 取字典中key對應的商品價格 if user_Money > amount: user_Money = user_Money - amount print "You need to pay RMB%s totally." % amount print "你的賬戶餘額爲: RMB%s" % user_Money ## 消費寫入日誌文件 drawtime = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) defname = sys._getframe().f_code.co_name Dlist = [cardId + '\t', drawtime + '\t', defname + ':' + list[k][0] + '\t', '-' + str(amount) + '\t', '0' + '\n'] day_log(Dlist) ## 將消費後金額寫入temp文件 tmp_Money = [] tmp_Money.append(user_Money) temp_log(Tlist=tmp_Money, content='dump') elif user_Money < amount: print "你的賬戶餘額爲: RMB%s" % user_Money print "你的賬戶餘額不足已支付本次購物,你可以嘗試選擇其他商品!" continue if (user_Money < 100) and (user_Money > 0): print "由於你的賬戶餘額已低於購物清單任何物品單價,系統自動退出購物,請充值後再來購買." sys.exit() elif yn_list == "N" or yn_list == "n": print "You input %s, exit buy!" % yn_list #sys.exit() break elif yn_list == "S" or yn_list == "s": while True: shop_id = raw_input("請輸入ID號,注意ID號不能重複:") shop_m = raw_input("請輸入商品名稱和價格,中間已單個空格.") list[shop_id] = shop_m print "成功添加商品到貨架." print list s_input = raw_input("是否繼續添加[Y/n]:") if s_input == "Y" or s_input == "y": continue else: print "Exit, 退出商品添加." break else: print "You input Other string, please again!" continue def day_log(Dlist): '''日記賬、每月賬單導出功能''' with open("log.txt", 'a') as df: df.writelines(Dlist) def temp_log(Tlist=None, content="load"): '''總金額臨時記錄文件''' try: if content == "load": loadput = open('temp.txt', 'r') loaddata = pickle.load(loadput) loadput.close() return loaddata elif content == "dump": output = open('temp.txt', 'wb') pickle.dump(Tlist, output, protocol=2) output.close() elif content == "loads": data = pickle.loads(Tlist) return data else: print "參數錯誤,重新輸入." except Exception, e: print e def repayment(cardID): '''信用卡還款接口''' try: while True: # 獲取初始額度 with open('F:\Python\Balance_tab','rb') as f: s = int(bf.read().split()[-1]) # 截止還款日獲取卡內餘額 balance = temp_log(content='load')[0] repay = s - balance if repay == 0: print "您本月已還款,無需還款." print "Exit, repayment." break else: print "您本月需要還款金額爲: ¥%s" % repay h_input = raw_input("確認是否還款,請輸入[Y/n]:") if h_input == "Y" or h_input == "y": amount_in = raw_input("請輸入您本次還款金額:") if float(amount_in) > repay: print "您輸入的還款額超過本月消費金額,請重新輸入." continue else: dumppay = float(amount_in) + balance # dump到temp文件 dpay = [dumppay] temp_log(Tlist=dpay, content='dump') print "還款成功." # 記錄到流水日誌 paytime = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) repadyLog = [cardID + '\t', paytime + '\t', "repayment" + '\t', '+' + amount_in + '\t', '0' + '\n'] day_log(repadyLog) elif h_input == "N" or h_input == "n": print "Exit, repayment system." break else: print "Please input again." continue except Exception, e: print e if __name__ == '__main__': main()