본문 바로가기
프로그램

[파이썬] 문제 : 사용자 정보 파일로 관리

by 오디세이99 2023. 4. 29.
728x90
반응형

 

filename = 'user.txt'         # 사용자 정보를 저장할 파일명
Accounts = {}                 # 사용자 딕셔너리

def readUser():               # 파일을 읽어서 딕셔너리  만듬
    f = open(filename, 'r')
    for L in f:
        tmp = L.split(':')
        Accounts[tmp[0]] = tmp[1].strip()
    f.close()

def writeUser():             # 딕셔너리를 파일로 저장
    f = open(filename, 'w')
    for k, v in Accounts.items():
        f.write(k + ':' + v + '\n')
    f.close()


readUser()                  # 처음 시작시 파일을 읽어 Accounts 딕셔너리를 만듬
while True: 
    msg = "-Type your ID\n"
    msg += "-If you want to make a new ID, please type 'MakeAccount' : "
    QuestionID = input(msg)
    if QuestionID in Accounts.keys(): 
        msg = 'Please type password for that ID : '
        QuestionPW = input(msg) 
        if QuestionPW == Accounts[QuestionID]: 
            print('Log in Success.') 
            break
    elif QuestionID == 'MakeAccount': 
        msg = 'Please type the name that you wnat to make for ID : '
        QuestionMID = input(msg) 
        msg = 'Please type the password that you want to make. : '
        QuestionMPW = input(msg) 
        # NewID = {QuestionMID : QuestionMPW} 
        print('Your ID is made. Congratulations!') 
        Accounts[QuestionMID] = QuestionMPW
        writeUser()                    # Accounts 딕셔너리를 파일로 저장
    elif QuestionID.upper() == 'Q':
        break

user.txt
0.00MB

applemac:AM
appleair:AA
applepro:AP

사용자 추가

728x90
반응형

댓글