- 과목별 점수를 입력하면 합격 여부를 출력하는 모듈을 만들어보자.
- 평균 60이상 합격, 과락 40으로 한다.
코드
def exampleResult(s1, s2, s3, s4, s5):
passAvgScore = 60
limitScore = 40
def getTotal():
totalScore = s1 + s2 + s3 + s4 + s5
print(f'총점: {totalScore}')
return totalScore
def getAverage():
avg = getTotal() / 5
print(f'평균: {avg}')
return avg
def printPassOrFail():
print(f'{s1}: Pass') if s1 >= limitScore else print(f'{s1}: Fail')
print(f'{s2}: Pass') if s2 >= limitScore else print(f'{s2}: Fail')
print(f'{s3}: Pass') if s3 >= limitScore else print(f'{s3}: Fail')
print(f'{s4}: Pass') if s4 >= limitScore else print(f'{s4}: Fail')
print(f'{s5}: Pass') if s5 >= limitScore else print(f'{s5}: Fail')
def printFinalPassOrFail():
if getAverage() >= passAvgScore:
if s1 >= limitScore and s2 >= limitScore and s3 >= limitScore and s4 >= limitScore and s5 >= limitScore:
print('Final Pass!!')
else:
print('Final Fail!!')
else:
print('Final Fail!!')
getAverage()
printPassOrFail()
printFinalPassOrFail()
코드
import passOrFail as pf
if __name__ == '__main__':
sub1 = int(input('과목1 점수 입력: '))
sub2 = int(input('과목2 점수 입력: '))
sub3 = int(input('과목3 점수 입력: '))
sub4 = int(input('과목4 점수 입력: '))
sub5 = int(input('과목5 점수 입력: '))
pf.exampleResult(sub1, sub2, sub3, sub4, sub5)
출력


코드
class NormalTv:
def __init__(self, i=32, c='black', r='full-HD'):
self.inch = i
self.color = c
self.resolution = r
self.smartTv = 'off'
self.aiTv = 'off'
def turnOn(self):
print('TV power on!!')
def turnOff(self):
print('TV power off!!')
def printTvInfo(self):
print(f'inch: {self.inch}inch')
print(f'color: {self.color}')
print(f'resolution: {self.resolution}')
print(f'smartTv: {self.smartTv}')
print(f'aiTv: {self.aiTv}')
class Tv4k(NormalTv):
def __init__(self, i, c, r='4k'):
super().__init__(i, c, r)
def setSmartTv(self, s):
self.smartTv = s
class Tv8k(NormalTv):
def __init__(self, i, c, r='8k'):
super().__init__(i, c, r)
def setSmartTv(self, s):
self.smartTv = s
def setAiTv(self, a):
self.aiTv = a
코드
import smartTv as st
if __name__ == '__main__':
my4KTv = st.Tv4k('65', 'silver', '4K')
my4KTv.setSmartTv('on')
my4KTv.turnOn()
my4KTv.printTvInfo()
my4KTv.turnOff()
print('-' * 60)
friend8KTv = st.Tv8k('86', 'red', '8K')
friend8KTv.setSmartTv('on')
friend8KTv.setAiTv('off')
friend8KTv.turnOn()
friend8KTv.printTvInfo()
friend8KTv.turnOff()
출력

- 회원가입 프로그램을 만들되 입력하지 않은 항목이 있는 경우 에러 메시지를 출력하는 프로그램을 만들어 보자.
코드
class EmptyDataException(Exception):
def __init__(self, i):
super().__init__(f'{i} is empty!')
def checkInputData(n, m, p, a, ph):
if n == '':
raise EmptyDataException('name')
elif m == '':
raise EmptyDataException('mail')
elif p == '':
raise EmptyDataException('password')
elif a == '':
raise EmptyDataException('address')
elif ph == '':
raise EmptyDataException('phone')
else:
return True
class RegisteMember():
def __init__(self, n, m, p, a, ph):
self.m_name = n
self.m_mail = m
self.m_pw = p
self.m_addr = a
self.m_phone = ph
print('Membership completed!!')
def printMemberInfo(self):
print(f'm_name: {self.m_name}')
print(f'm_mail: {self.m_mail}')
print(f'm_pw: {self.m_pw}')
print(f'm_addr: {self.m_addr}')
print(f'm_phone: {self.m_phone}')
import mem
m_name = input('이름 입력: ')
m_mail = input('메일 주소 입력: ')
m_pw = input('비밀번호 입력: ')
m_addr = input('주소 입력: ')
m_phone = input('연락처 입력: ')
try:
mem.checkInputData(m_name, m_mail, m_pw, m_addr, m_phone)
newMember = mem.RegisteMember(m_name, m_mail, m_pw, m_addr, m_phone)
newMember.printMemberInfo()
except mem.EmptyDataException as e:
print(e)
출력

- 텍스트 파을에 수입과 지출을 기록하는 가계부를 만들어보자.
코드
import time
limitSpendMoney = 2000
def getTime():
lt = time.localtime()
st = time.strftime('%Y-%m-%d %H:%M:%S')
return st
while True:
selectNumber = int(input('1.입금 \t 2.출금 \t 3.종료 '))
if selectNumber == 1:
money = int(input('입금액 입력: '))
with open('/Users/yinjun/pythonTxt/bank/money.txt', 'r') as f:
m = f.read()
with open('/Users/yinjun/pythonTxt/bank/money.txt', 'w') as f:
f.write(str(int(m) + money))
memo = input('입금 내역 입력: ')
with open('/Users/yinjun/pythonTxt/bank/pocketMoneyRegister.txt', 'a') as f:
f.write('-------------------------------------------\n')
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 selectNumber == 2:
money = int(input('출금액 입력: '))
with open('/Users/yinjun/pythonTxt/bank/money.txt', 'r') as f:
m = f.read()
with open('/Users/yinjun/pythonTxt/bank/money.txt', 'w') as f:
f.write(str(int(m) - money))
memo = input('출금 내역 입력: ')
with open('/Users/yinjun/pythonTxt/bank/pocketMoneyRegister.txt', 'a') as f:
f.write('-------------------------------------------\n')
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 selectNumber == 3:
print('Bye~')
break
else:
print('잘못 입력하셨습니다.')
출력
