python用户注册函数

db_path='db.txt'

def get_username( ):
    while True:
        username = input('please input username:')
        if not username.isalpha():continue
        with open(r'%s'%db_path, 'r', encoding='utf-8')as f:
            for line in f:
                userinfo = line.strip('\n').split(',')
                if username == userinfo[0]:
                    print('The username already exist!')
                    break
            else:
                return  username

def get_pwd():
    while True:
        pwd = input('Please input password:').strip()
        pwd1 = input('Please confirm password:').strip()
        if pwd == pwd1:
            return pwd
        else:
            print('These two password are node the same')

def get_balance():
    while True:
        balance = input('Please charge:').strip()
        if balance.isdigit():
            return balance
        else:
            print('Please input a digit')

def file_handle(username, pwd, balance):
    with open(db_path, 'a', encoding='utf-8')as f:
        f.write('%s,%s,%s\n'%(username,pwd,balance))

def register():
    username = get_username()
    pwd = get_pwd()
    balance = get_balance()
    file_handle(username, pwd, balance)

if __name__ == '__main__':
    register()

C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
please input username:raimond
The username already exist!
please input username:lily
Please input password:alex
Please confirm password:alex
Please charge:251

Process finished with exit code 0

你可能感兴趣的:(python)