230414_제로베이스데이터스쿨_파이썬중급문풀_05058부터05060까지

김지태·2023년 4월 17일
1
post-thumbnail

모듈 연습

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'해상도: {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

05_058 클래스06 자동차 경주 게임

모듈 파일 - car.py

import random

class Car:

def __init__(self, n='fire car', c='red', s=200):
    self.name = n
    self.color = c
    self.max_speed = s
    self.distance = 0

def printCarInfo(self):
    print(f'name: {self.name}, '
          f'color: {self.color}, '
          f'max_speed: {self.max_speed}, '
          f'current_speed: {self.cur_speed}')

def controlSpeed(self):
    return random.randint(0, self.max_speed)

def getDistanceForHour(self):
    return self.controlSpeed() * 1

if name == 'main':
tempCar = Car('myCar', 'black', 250)
tempCar.printCarInfo()
print(tempCar.controlSpeed())

모듈 파일 - racing.py

from time import sleep

class CarRacing:

def __init__(self):
    self.cars = []
    self.rankings = []

def startRacing(self):
    for i in range(10):
        print(f'Racing: {i+1}바퀴')
        for car in self.cars:
            car.distance += car.getDistanceForHour()

        sleep(1)
        self.printCurrentCarDistance()

def printCurrentCarDistance(self):
    for car in self.cars:
        print(f'{car.name}: {car.distance}\t\t', end='')
    print()

def addCar(self, c):
    self.cars.append(c)
    
    

실행파일

from car_game import racing as rc
from car_game import car

myCarGame = rc.CarRacing()
car01 = car.Car('Car01', 'white', 250)
car02 = car.Car('Car02', 'black', 200)
car03 = car.Car('Car03', 'yellow', 220)
car04 = car.Car('Car04', 'red', 280)
car05 = car.Car('Car05', 'blue', 150)

myCarGame.addCar(car01)
myCarGame.addCar(car02)
myCarGame.addCar(car03)
myCarGame.addCar(car04)
myCarGame.addCar(car05)

myCarGame.startRacing()

05_059 클래스7 - mp3 플레이어

모듈파일 - mp3player.py

import random
from time import sleep

class Song:

def __init__(self, t, s, pt):  
    self.title = t
    self.singer = s
    self.play_time = pt

def printSongInfo(self):
    print(f'Title: {self.title}, Singer: {self.singer}, Play time: {self.play_time}')

class Player:

def __init__(self):
    self.songList = []
    self.isLoop = False

def addSong(self, s):
    self.songList.append(s)

def play(self):
    if self.isLoop:
        while self.isLoop:
            for s in self.songList:
                print(f'Title: {s.title}, Singer: {s.singer}, Play time: {s.play_time}sec')
                sleep(s.play_time)
    else:
        for s in self.songList:
            print(f'Title: {s.title}, Singer: {s.singer}, Play time: {s.play_time}sec')
            sleep(s.play_time)

def suffle(self):
    random.shuffle(self.songList)

def setIsLoop(self, flag):
    self.isLoop = flag

실행 파일

import mp3player as mp3

s1 = mp3.Song('신호등', '이무진', 3)
s2 = mp3.Song('Permission to Dance', '방탄소년단', 4)
s3 = mp3.Song('Butter', '방탄소년단', 2)
s4 = mp3.Song('Weekend', 'TAEYEON', 5)
s5 = mp3.Song('좋아좋아', '조정석', 4)

player = mp3.Player()
player.addSong(s1)
player.addSong(s2)
player.addSong(s3)
player.addSong(s4)
player.addSong(s5)

player.setIsLoop(False)
player.suffle()
player.play()

05_060 예외처리 1

모듈 파일 - calculator.py

def add(n1, n2):
print('덧셈 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

print(f'{n1} + {n2} = {n1 + n2}')

def sub(n1, n2):
print('뺄셈 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

print(f'{n1} - {n2} = {n1 - n2}')

def mul(n1, n2):
print('곱셈 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

print(f'{n1} * {n2} = {n1 * n2}')

def div(n1, n2):
print('나눗셈 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

if n2 == 0:
    print('0으로 나눌 수 없습니다.')
    return

print(f'{n1} / {n2} = {n1 / n2}')

# try:
#     print(f'{n1} / {n2} = {n1 / n2}')
# except ZeroDivisionError as e:
#     print(e)
#     print('0으로 나눌 수 없습니다.')

def mod(n1, n2):
print('나머지 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

if n2 == 0:
    print('0으로 나눌 수 없습니다.')
    return

print(f'{n1} % {n2} = {n1 % n2}')

def flo(n1, n2):
print('몫 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

if n2 == 0:
    print('0으로 나눌 수 없습니다.')
    return

print(f'{n1} // {n2} = {int(n1 // n2)}')

def exp(n1, n2):
print('거듭제곱 연산')
try:
n1 = float(n1)
except:
print('첫 번째 피연산자는 숫자가 아닙니다.')
return

try:
    n2 = float(n2)
except:
    print('두 번째 피연산자는 숫자가 아닙니다.')
    return

print(f'{n1} ** {n2} = {n1 ** n2}')

실행 파일

import calculator as cc

num1 = input('첫 번째 피연산자 입력: ')
num2 = input('두 번째 피연산자 입력: ')

cc.add(num1, num2)
cc.sub(num1, num2)
cc.mul(num1, num2)
cc.div(num1, num2)
cc.mod(num1, num2)
cc.flo(num1, num2)
cc.exp(num1, num2)

profile
데이터 분석가

0개의 댓글