用函數寫註冊功能
阿新 • • 發佈:2018-07-22
hid ide {} spl 學習 else gist 註冊功能 用戶
昨天看了函數的視屏並完成了作業,與大家分享一下。只想為自己記錄學習的過程,也希望大家指出我的不足和錯誤,你們的意見是我學習動力!謝謝!
作業: 用函數寫用戶註冊功能1 def get_userinfo(): 2 ‘‘‘ 3 讀取用戶信息 4 :return: 5 ‘‘‘ 6 user_info_dict={} 7 with open(‘db‘,‘r‘,encoding=‘utf-8‘) as f: 8 for line in f: 9 line = line.strip().split(‘View Code,‘) 10 user_info_dict[line[0]] ={ 11 ‘password‘:line[1], 12 ‘balance‘:line[2], 13 } 14 return user_info_dict 15 def get_name(): 16 ‘‘‘ 17 輸入用戶名 18 :return: 19 ‘‘‘ 20 while True: 21 name = input(‘用戶名>>:‘).strip()22 if name.isalpha(): 23 if name not in get_userinfo(): 24 return name 25 else: 26 print(‘用戶名已存在!‘) 27 else: 28 print(‘用戶名輸入不合法!‘) 29 def get_pwd(): 30 ‘‘‘ 31 輸入密碼 32 :return: 33 ‘‘‘ 34 while True:35 pwd1 = input(‘請輸入密碼>>:‘).strip() 36 pwd2 = input(‘請再次輸入密碼>>:‘).strip() 37 if pwd1 == pwd2: 38 return pwd1 39 else: 40 print(‘兩次輸入密碼不一致,請重新輸入!‘) 41 def get_bal(): 42 ‘‘‘ 43 輸入金額 44 :return: 45 ‘‘‘ 46 bal = input(‘請輸入金額>>:‘).strip() 47 if bal.isdigit(): 48 return bal 49 else: 50 print(‘金額必須是數字‘) 51 def register(): 52 ‘‘‘ 53 註冊功能 54 :return: 55 ‘‘‘ 56 get_userinfo() 57 username = get_name() 58 password = get_pwd() 59 salary = get_bal() 60 user_info = [username,password,salary] 61 with open(‘db‘,‘a‘,encoding=‘utf-8‘)as f: 62 f.write(‘,‘.join(user_info)+‘\n‘) 63 print(‘用戶註冊成功!!!‘) 64 65 register()
下面是文本db
1 alex,abc,1500 2 egon,asd,5200 3 jack,123,2300
用函數寫註冊功能