회원계정별 텍스트파일을 생성한 후 회원 본일 파일에 한줄 읽기를 쓰고 읽는 프로그램을 만들어보자
import time
url = 'C:/pythonTXT/'
class Program :
def join(self):
input_id = str(input('input ID : '))
input_pw = str(input('input PW : '))
with open(url + 'program.txt','a') as jw :
jw.write('ID : ' + input_id+'\n')
jw.write('ID : ' + input_pw+'\n')
temp_dic = {}
temp_dic[input_id] = input_pw
with open(url + 'program_'+input_id+'.txt','w') as jp :
jp.writelines( key + ':' + value + '\n'
for key,value in temp_dic.items())
with open(url + 'program.txt','r') as jr :
print(jr.read())
def log(self):
input_id = str(input('input ID : '))
input_pw = str(input('input PW : '))
with open(url + 'program_'+input_id+'.txt','r') as l :
read_dic = {}
line = l.readline()
temp_list = line.split(':')
read_dic[temp_list[0]] = temp_list[1].strip('\n')
for key, value in read_dic.items() :
if key == input_id and value == input_pw :
print('login success')
note = input('오늘 하루 인상깊은 일을 기록하세요. :')
with open(url + 'program_'+input_id+'_log.txt','a') as lr :
local_time = time.strftime('%Y-%m-%d %I:%M:%S %p')
lr.write('[' + local_time + ']' + '\t' + note + '\n')
else :
print('login fail')
def read_log(self):
input_id = str(input('input ID : '))
input_pw = str(input('input PW : '))
with open(url + 'program_' + input_id + '.txt', 'r') as rl:
read_dic = {}
line = rl.readline()
temp_list = line.split(':')
read_dic[temp_list[0]] = temp_list[1].strip('\n')
for key, value in read_dic.items():
if key == input_id and value == input_pw:
print('login success')
with open(url + 'program_' + input_id + '_log.txt', 'r') as rlr :
result = rlr.readline().strip('\n')
while result != '' :
print(result)
result = rlr.readline().strip('\n')
else :
print('login fail')
while True :
choose = int(input('1.회원가입\t2.한줄일기쓰기\t3.한줄일기보기\t4.종료 : '))
if choose == 1 :
Program().join()
elif choose == 2 :
Program().log()
elif choose == 3 :
Program().read_log()
elif choose == 4 :
break
강의 문제풀이와 완전히 다르게 짰다.
회원가입 파일과 각각 회원마다의 id,pw가 있는 파일, 그리고 해당 회원만의 일지파일 세가지를 만들고
회원가입시 회원가입 파일에 저장하는 메서드와
일지 작성 시 id/pw 파일과 비교하여 로그인에 성공하면 일지파일에 작성하는 메서드
그리고 같은방식으로 로그인에 성공하면 일지파일을 읽는 메서드가 들어있는
클래스를 만든 후 실행 시키는 방식이다.
중복되는 코드가 몇 있어 이부분을 변수에 담으면 좀 더 간결해 질것같다.
readline 함수 실행시 개행문구가 담겨오는데,
strip메서드를 잘 활용해야겠다.
텍스트파일에 수입과 지출을 기록하는 가계부
import time
url = 'C:/pythonTXT/log.txt'
local_time = time.strftime('%Y-%m-%d %I:%M:%S')
class Housekeeping_Book :
def __init__(self):
self.moeny = 0
def deposit(self):
money = input('입금액 입력 : ')
log = input('입금 내역 입력 : ')
with open(url ,'a') as f :
global local_time
f.write('-----------------------\n')
f.write('[' + local_time + ']' + '\n')
self.moeny += int(money)
f.write(f'[입금] {log} : {self.moeny}원\n')
f.write(f'[잔액] : {self.moeny}원\n')
def drawal(self):
money = input('출금액 입력 : ')
log = input('출금 내역 입력 : ')
with open(url, 'a') as f:
global local_time
f.write('-----------------------\n')
f.write('[' + local_time + ']' + '\n')
self.moeny -= int(money)
f.write(f'[출금] {log} : {self.moeny}원\n')
f.write(f'[잔액] : {self.moeny}원\n')
a = Housekeeping_Book()
while True :
i = int(input('1.입금\t2.출금\t3.종료 : '))
if i == 1 :
a.deposit()
elif i == 2 :
a.drawal()
elif i == 3 :
break
계속해서 class 복습겸 인스턴스 생성 후 입력되게끔 짜고있다.
짜다보니 만약 은행 클래스라면
고객 한명한명의 텍스트파일을 따로 생성해야겠다는 생각이 든다.
고객 정보 테이블이 있다면 거기서 이름과 생년월일을 비교해서
해당고객의 입출금을 기록하는 방식으로?
다음에 시간나면 한번 짜봐야겠다.