(중급) problems

임경민·2023년 9월 28일
1
post-thumbnail
post-custom-banner

Summarization


  • Python 중급 문제풀이
  • 클래스, 예외처리, 텍스트파일 문제풀이 진행

@abstractmethod : 강제 사용 Method
random : 난수 발생 모듈
time : 시간 관련 모듈


Contents


  • 클래스
class Book:

    def __init__(self, name, price, isbn):
        self.bookName = name
        self.bookPrice = price
        self.bookIsbn = isbn

class BookRepository:
    def __init__(self):
        self.bDic = {}

    def registBook(self, b):
        self.bDic[b.bookIsbn] = b

    def removeBook(self, isbn):
        del self.bDic[isbn]

    def printBooksInfo(self):
        for isbn in self.bDic.keys():
            b = self.bDic[isbn]
            print(f'{b.bookName}, {b.bookPrice}, {b.bookIsbn}')

    def printBookInfo(self, isbn):
        if isbn in self.bDic:
            b = self.bDic[isbn]
            print(f'{b.bookName}, {b.bookPrice}, {b.bookIsbn}')
        else:
            print('Lookup result does not exist.')
  • 예외처리 : try ~ except ~ finally 구분
import bank

koreaBank = bank.Bank()

new_account_name = input('통장 개설을 위한 예금주 입력: ')
myAccount = bank.PrivateBank(koreaBank, new_account_name)
myAccount.printBankInfo()

while True:

    selectNumber = int(input('1. 입금 \t 2. 출금 \t 3. 종료'))
    if selectNumber == 1:
        money = int(input('입금액 입력: '))
        koreaBank.doDeposit((myAccount.account_no, money))
        myAccount.printBankInfo()

    elif selectNumber == 2:
        money = int(input('출금액 입력: '))
        try:
            koreaBank.doWithdraw(myAccount.account_no, money)

        except bank.LackException as e:
            print(e)

        finally:
            myAccount.printBankInfo()

    elif selectNumber == 3:
        print('Bye~')
        break

    else:
        print('잘못 선택했습니다. 다시 선택하세요')
  • 텍스트파일 : 'r' : 읽기 'w' : 쓰기 'a' : 덮어쓰기
    'w'는 초기화하며 작성되나, 'a'는 이전내용에 이어 작성
url = '/pythonEx/project/PythonTxt/'

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 + i)):
    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(2023, 1, 1, 10, 0, 0)
with open(url + 'shipAlive.txt', 'a') as f:
    f.write(f'2023년 모든 선박 입항일\n')
    f.write(f'{baseTime}\n')

nextTime = baseTime + timedelta(days = minDay)
while True:

    with open(url + 'shipAlive.txt', 'a') as f:
        f.write(f'{nextTime}\n')

    nextTime += timedelta(days = minDay)
    if nextTime.year >= 2024:
        break
post-custom-banner

0개의 댓글