python小作业初版之信用卡交易

完全没有异常处理。还在学习中。。。部分代码还能再写成函数。

#!/usr/bin/python
import sys
import pickle
import os
import getpass
import time
def login(username = '',password = '',user_type = 1):
    print 'Welcome 2 ATM Basic System.'
    error_count = 0
    initInfo(info_list)
    pkl_file = file('userInfo.pkl','rb')
    dic_account = pickle.load(pkl_file)
    pkl_file.close()
    while True:
        username = str(raw_input('please input your username or quit(q): ').strip())
        if len(username) == 0:
            print 'Invalid Username!'
        elif username in ('q','quit'):
            sys.exit()
        elif username not in dic_account.keys():
            print 'No %s found!' % username
        else:
            password = str(dic_account[username][0])
            type = int(dic_account[username][-1])
            if type == 2:
                print 'Account %s is Lock!Please contact your Admin.' % username
                sys.exit()
            else:
                while True:
                    pass_input = getpass.getpass('please input your password: ')
                    if pass_input != password:
                        print 'Wrong Password!'
                        error_count += 1
                        if error_count == 3:
                            print 'You give the wrong password 3 times.now exit!'
                            dic_account[username][-1] = 2
                            dicDump(dic_account)
                            sys.exit()
                        else:
                            continue
                    else:
                        if type == 1:
                            while True:
                                print 'Welcome,%s' % username
                                print '*'*20
                                print '1.Go Shopping'
                                print '2.Withdraw'
                                print '3.Repay'
                                print '4.Show Detail'
                                print '0.quit'
                                print '*'*20
                                choice = int(raw_input('Please choose one option: ').strip())
                                if choice == 1:
                                    goShopping(username,dic_account)
                                elif choice == 2:
                                    withDraw(username,dic_account)
                                elif choice == 3:
                                    rePay(username,dic_account)
                                elif choice == 4:
                                    showDetail(username,dic_account)
                                elif choice == 0:
                                    sys.exit()
                                else:
                                    print 'please choose one option with int 1/2/3/4/0.'
                        else:
                            while True:
                                print 'Welcome!Admin %s' % username
                                print '*'*20
                                print '1.Add User'
                                print '2.Edit User'
                                print '3.Show All'
                                print '0.quit'
                                print '*'*20
                                choice = int(raw_input('Please choose one option: ').strip())
                                if choice == 1:
                                    addUser(dic_account)
                                elif choice == 2:
                                    editUser(dic_account)
                                elif choice == 3:
                                    showAll(dic_account) 
                                elif choice == 0:
                                    sys.exit()
                                else:
                                    print 'please choose one option with int 1/2/3/4/0.'
def initInfo(info_list):
    if not os.path.exists('userInfo.pkl'):
        pkl_file = open('userInfo.pkl','wb')
        pickle.dump(info_list,pkl_file)
        pkl_file.close()
    else:
        pass
def dicDump(dic_account):
    pkl_file = open('userInfo.pkl','wb')
    pickle.dump(dic_account,pkl_file)
    pkl_file.close()
        
def goShopping(username,dic_account):
    shopList = {'Computer':4999,'Phone':3999,'Music Player':1688,'Coffee':27}
    for item in shopList:
        print '%s => %s' % (item,shopList[item])
    while True:
        each = raw_input('which one are you gonna buy?').strip()
        if each in ('q','quit','exit'):
            break
        elif each not in shopList.keys():
            print 'No %s in the shopping list!retry!' % each
        else:
            if dic_account[username][2] >= shopList[each]:
                f = file('detail.log','a')
                f.write('%s\t%s\t%s\t%s\t0\n' % (username,time.strftime('%y-%m-%d %H:%M:%S',time.localtime()),each,shopList[each]))
                f.close()
                dic_account[username][2] -= shopList[each]
                dicDump(dic_account)
                print 'You have bought %s,it takes you %d yuan.now you have a balance of %d.' % (each,shopList[each],dic_account[username][-2])
            else:
                print 'You have not enough money to offord it!'
                break
def withDraw(username,dic_account):
    draw_count = int(raw_input('How much Cash withdrawal? '))
    while True:
        if (draw_count*105/100) > dic_account[username][2]:
            print 'Oops!You have not enough Amount to Cash withdrawal!Your balance now is %s !'% (dic_account[username][2])
            break
        else:
            dic_account[username][2] -= draw_count
            dicDump(dic_account)
            f = file('detail.log','a')
            f.write('%s\t%s\tCash withdrawal\t%s\t%s\n' % (username,time.strftime('%y-%m-%d %H:%M:%S',time.localtime()),draw_count,(draw_count*5/100)))
            f.close()
            print 'For now,your balance is %d'% (dic_account[username][2])
            break
def rePay(username,dic_account):
    save_count = int(raw_input('How much Cash to save? '))
    dic_account[username][2] += save_count
    dicDump(dic_account)
    f = file('detail.log','a')
    f.write('%s\t%s\tRepay\t%s\t0\n' % (username,time.strftime('%y-%m-%d %H:%M:%S',time.localtime()),save_count))
    f.close()
    print 'For now,your balance is %d'% (dic_account[username][2])
def showDetail(username,dic_account):
    if not os.path.exists('detail.log'):
        print 'Your Detail List is Empty!'
        print 'Your max permit is %s,now your balance is %s.\n'%(dic_account[username][1],dic_account[username][2])
    else:
        print 'Your max permit is %s,now your balance is %s.\n'%(dic_account[username][1],dic_account[username][2])
        f = file('detail.log','r')
        for eachLine in f:
            if eachLine.startswith(username):
                print eachLine
            else:
                pass
        f.close()
def addUser(dic_account):
    add_username = str(raw_input('What\'s the user\'s name? ').strip())
    if len(add_username) == 0:
        print 'Invalid User Name!'
    elif add_username.lower() in dic_account.keys():
        print 'User Already Exists@!'
    else:
        while True:
            add_passwd = getpass.getpass('Please input the password: ')
            if len(add_passwd) < 6:
                print 'Password\'s len must longer than 6 letters!'
                break
            else:
                add_passwd_again = getpass.getpass('Please input the password again: ')
                if add_passwd_again != add_passwd:
                    print 'password is\'t same as the first time input!'
                    break
                else:
                    add_user_balance = int(raw_input('Define a max balance number: '))
                    dic_account[add_username] = [add_passwd,add_user_balance,add_user_balance,1]
                    dicDump(dic_account)
                    print 'Add %s success!Amount is %d.'% (add_username,add_user_balance)
                    break
def editUser(dic_account):
    edit_username = str(raw_input('Which user do you want to edit? ').strip())
    if len(edit_username) == 0:
        print 'Invalid User Name!'
    elif edit_username.lower() not in dic_account.keys():
        print 'User Doesn\'t Exists@!'
    else:
        while True:
            print '\n\t1.Change The Max Balance'
            print '\t2.Change Password'
            print '\t3.Lock/Unlock The User'
            edit_choice = int(raw_input('Please Choose one option or quit!: '))
            if edit_choice == 1:
                edit_user_balance = int(raw_input('Define a max balance number to Change: '))
                dic_account[edit_username][1] = edit_user_balance
                dicDump(dic_account)
                break
            elif edit_choice == 2:
                edit_user_passwd = getpass.getpass('Please input the password : ')
                dic_account[edit_username][0] = edit_user_passwd
                dicDump(dic_account)
                break
            elif edit_choice == 3:
                edit_user_type = int(raw_input('Lock => 2,Unlock => 1: '))
                dic_account[edit_username][-1] = edit_user_type
                dicDump(dic_account)
                break
            else:
                print 'Please retry,just use the int 1/2/3!'
                break      
def showAll(dic_account):
    for each_user in dic_account.keys():
        print '*'*20
        print '%s\'s Max Amount is %d,now Balance is %d.\n' % (each_user,dic_account[each_user][1],dic_account[each_user][2])
        f = file('detail.log','r')
        for eachLine in f:
            if eachLine.startswith(each_user):
                print eachLine
            else:
                pass
        f.close()
info_list = {
        'fengxsong':['p@55w0rd',15000,15000,0],
        'nagios':['nagios',9000,9000,1]
    }
if __name__ == '__main__':
    login()


你可能感兴趣的:(python,信用卡)