
1. 다이어리

(diary.py)
import time
def writeDiary(u, f, d):
lt = time.localtime()
timeStr = time.strftime('%Y-%m-%d %I:%M:%s %p', lt)
filtPath = u + f
with open(filtPath, 'a') as f:
f.write(f'[{timeStr}] {d}\n')
def readDiary():
filtPath = u + f
datas = []
with open(filtPath, 'r') as f:
datas = f.readlines()
return datas
(ex.py)
import diary
members = {}
uri = 'C:\Users'
def printMembers():
for m in members.keys():
print(f'ID: {m} \t PW: {members[m]}')
while True:
selectNum = int(input('1.회원가입 2.한줄일기쓰기 3.일기보기 4.종료'))
if selectNum == 1:
mId = input('input ID: ')
mPw = input('input PW: ')
members[mId] = mPw
elif selectNum == 2:
mId = input('input ID: ')
mPw = input('input PW: ')
if mId in members and members[mId] == mPw:
print('login success!!')
fileName = 'myDiary_' + mId + '.txt'
data = input('오늘 인상 깊은 일을 기록하세요')
diary.writeDiary(uri, fileName, data)
else:
print('login fail!!')
printMembers()
elif selectNum == 3:
mId = input('input ID: ')
mPw = input('input PW: ')
if mId in members and members[mId] == mPw:
print('login success!!')
fileName = 'myDiary_' + mId + '.txt'
datas = diary.readDiary(uri, fileName)
for d in datas:
print(d, end="")
else:
print('login fail!!')
printMembers()
elif selectNum == 4:
print('Bye~~')
break
2. 가계부
(ex.py)
import time
def getTime():
lt = time.localtime()
st = time.strftime('%Y-%m-%d %H:%M:%S')
return st
while True:
selectNum = int(input('1.입금 2.출금 3.종료 '))
if selectNum == 1:
money = int(input('입금액 입력: '))
with open('C:/pythonTxt/bank/money.txt', 'r') as f:
m = f.read()
with open('C:/pythonTxt/bank/money.txt', 'w') as f:
m = f.write(str(int(m) + money))
memo = input('입금내역입력: ')
with open('C:/pythonTxt/bank/moneyRegister.txt', 'w') as f:
f.write('--------------------------------')
f.write(f'{getTime()} \n' )
f.write(f'[입금] {memo} : {str(money)}원 \n')
f.write(f'[잔액] {str(int(m) + money)}원 \n')
print('입금완료!!')
print(f'기존 잔액: {m}')
print(f'입금 후 잔액: {int(m) + money)}원')
elif selectNum == 2:
money = int(input('출금액 입력: '))
with open('C:/pythonTxt/bank/money.txt', 'r') as f:
m = f.read()
with open('C:/pythonTxt/bank/money.txt', 'w') as f:
m = f.write(str(int(m) - money))
memo = input('출금내역입력: ')
with open('C:/pythonTxt/bank/moneyRegister.txt', 'w') as f:
f.write('--------------------------------')
f.write(f'{getTime()} \n')
f.write(f'[출금] {memo} : {str(money)}원 \n')
f.write(f'[잔액] {str(int(m) - money)}원 \n')
print('출금완료!!')
print(f'기존 잔액: {m}')
print(f'입금 후 잔액: {int(m) - money)}원')
elif selectNum == 3:
print('Bye~~')
break
else:
print('다시 입력하세요!!')
3. 약수, 소수 기록
#약수
inputNumber = int(input('0보다 큰 정수 입력: '))
divisor = []
for number in range(1, (inputNumber + 1)):
if inputNumber % number == 0:
divisor.append(number)
if len(divisor) > 0:
try:
with open('C:/pythonTxt/divisor.txt', 'a') as f:
f.write(f'{inputNumber}의 약수: ')
f.write(f'{divisor}\n')
except Exception as e:
print(e)
else:
print('divisor write complete!!')
#소수
inputNumber = int(input('0보다 큰 정수 입력: '))
prime = []
for number in range(2, (inputNumber + 1)):
flag = True
for n in range(2, number):
if number % n == 0:
flag = False
break
if len(prime) > 0:
try:
with open('C:/pythonTxt/divisor.txt', 'a') as f:
f.write(f'{inputNumber}까지의 소수: ')
f.write(f'{prime}\n')
except Exception as e:
print(e)
else:
print('prime write complete!!')
4. 공약수 기록
#공약수
n1 = int(input('1보다 큰 정수 입력: '))
n2 = int(input('1보다 큰 정수 입력: '))
common = []
for i in range(1, (n1+1)):
if n1 % i == 0 and n2 % i == 0:
common.append(i)
if len(common) > 0:
try:
with open('C:/pyhtonTxt/common.txt', 'a') as f:
f.write(f'{n1}과 {n2}의 공약수: ')
f.write(f'{common}\n')
except Exception as e:
print(e)
else:
print('common factor write complete!!')
5. 배 입항일(공배수)
ship1 = 3
ship2 = 4
ship3 = 5
maxDay = 0
for i in range(1, (ship1+1)):
if ship1 % i == 0 and ship2 % i == 0:
maxDay = i
minDay = (ship1 * ship2) // maxDay
newDay = minDay
for i in range(1, (newDay + 1)):
if newDay % i == 0 and ship3 % i == 0:
maxDay = i
minDay = (newDay * ship3) // maxDay
print(f'minDay: {minDay}')
print(f'maxDay: {maxDay}')
from datetime import datetime
from datetime import timedelta
n = 1
baseTime = datetime(2021, 1, 1, 10, 0, 0)
with open('C:/pythonTxt/arrive.txt', 'a') as f:
f.write(f'2021년 모든 선박 입항일\n')
f.write(f'{baseTime}\n')
nextTime = baseTime + timedelta(days=minDay)
while True:
with open('C:/pythonTxt/arrive.txt', 'a') as f:
f.write(f'{nextTime}\n')
nextTime = nextTime + timedelta(days=minDay)
if nextTime.year > 2021:
bread