Python 문풀 06.

yoong·2023년 5월 1일

1. Python

목록 보기
19/21

[실습 01]

:자동차 경주 게임

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}, color: {self.color}, max_speed: {self.max_speed}')

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

    #속도 * 시간
    def getDistanceForHour(self):
        return self.controlSpeed() * 1



#모듈
from time import sleep #잠시 슬립할수 있음.

class CarRacing:

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

#10바퀴 1초
    def starRacing(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 Class import carRace as cr
from Class import carRaceModule as crm

#자동차 객체 만듬
myCarGame = crm.CarRacing()
car01 = cr.Car('Car01','White',250)
car02 = cr.Car('Car02','black',200)
car03 = cr.Car('Car03','red',150)
car04 = cr.Car('Car04','blue',300)
car05 = cr.Car('Car05','yellow',280)

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

myCarGame.starRacing()

[실습 02]

import mp3Module as mp3

song1 = mp3.Song("신호등","이무진", 3)
song2 = mp3.Song("피땀눈물","방탄", 7)
song3 = mp3.Song("난빛나","제로베이스원", 4)
song4 = mp3.Song("손오공","세븐틴", 2)
song5 = mp3.Song("버터","방탄소년단", 3)

player = mp3.Player()
player.addSong(song1)
player.addSong(song2)
player.addSong(song3)
player.addSong(song4)
player.addSong(song5)

player.setIsLoop(False)
player.shuffle()
player.play()
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 shuffle(self):
        random.shuffle(self.songList)

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

[실습 03]

#덧셈
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

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

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


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

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

cm.add(num1,num2)
cm.div(num1,num2)

Reference

  • 이글은 제로베이스 데이터 취업 스쿨의 강의자료 일부를 발췌하여 작성되었음.
profile
데이터와 이미지로 세상을 공부하는 중입니다 :)

0개의 댓글