例項
#系統輸入密碼錯誤3次就關閉系統 count = 0 flag = False while count < 3: _name = input("username:") _psw = input("password:") f = open('users.txt','r') lines = f.readlines() for line in lines: index = line.index(',') name = line[0:index] password = line[index + 1:-1] # print(name,password) if _name == name and _psw == password: flag = True break else: flag = False f.close() if flag: print("歡迎進入系統。。。。") break else: print("輸入的使用者或者密碼不正確。。。。") count +=1 else: print("你輸入的次數太多,系統關閉!")
users.txt中資料格式
0001,123
0002,452