Coding 63

[python] ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ€ด์ฆˆ

#์ž๋™ ์ฃผ๋ฌธ ์‹œ์Šคํ…œ: ์‹œ์Šคํ…œ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์ ์ ˆํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์„ ๋„ฃ์œผ์‹œ์˜ค # ์กฐ๊ฑด1: 1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜ฌ๋•Œ๋Š” ValueError๋กœ ์ฒ˜๋ฆฌ # ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€ : "์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. " # ์กฐ๊ฑด2: ๋Œ€๊ธฐ ์†๋‹˜์ด ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์น˜ํ‚จ๋Ÿ‰์€ 10๋งˆ๋ฆฌ๋กœ ํ•œ์ • # ์น˜ํ‚จ ์†Œ์ง„์‹œ ์‚ฌ์šฉ์ž ์ •์˜ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ # ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€: "์žฌ๊ณ ๊ฐ€ ์†Œ์ง„๋˜์–ด ๋” ์ด์ƒ ์ฃผ๋ฌธ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค." class TooManyChickenError(Exception): pass chicken = 10 waiting = 1 #ํ™€ ์•ˆ์—๋Š” ๋งŒ์„ ๋Œ€๊ธฐ๋ฒˆํ˜ธ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ while(True): try: print("๋‚จ์€ ์น˜ํ‚จ : {0}".format(chicken)) order = int(input("์น˜..

Coding/Python 2021.11.04

[python] ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

try: print("๋‚˜๋ˆ„๊ธฐ ์ „์šฉ ๊ณ„์‚ฐ๊ธฐ์ž…๋‹ˆ๋‹ค/") nums = [] nums.append(int(input("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” :"))) nums.append(int(input("๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” :"))) nums.append(int(nums[0]/nums[1])) print("{0} / {1} = {2}".format(nums[0], nums[1], nums[2])) except ValueError: print("์—๋Ÿฌ! ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.") except ZeroDivisionError as err: print(err) except Exception as err: print("์•Œ ์ˆ˜ ์—†๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ") print(err) #ํ•œ ์ž๋ฆฌ ์ˆซ์ž ์ „์šฉ ๋‚˜๋ˆ—์…ˆ ๊ณ„์‚ฐ๊ธฐ ..

Coding/Python 2021.11.04

[python] ํด๋ž˜์Šค ํ€ด์ฆˆ

# ์ด 3๋Œ€์˜ ๋งค๋ฌผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. # ๊ฐ•๋‚จ ์•„ํŒŒํŠธ ๋งค๋งค 10์–ต 2010๋…„ # ๋งˆํฌ ์˜คํ”ผ์Šคํ…” ์ „์„ธ 5์–ต 2007๋…„ # ์†กํŒŒ ๋นŒ๋ผ ์›”์„ธ 500/50 2000๋…„ # class House: #๋งค๋ฌผ ์ดˆ๊ธฐํ™” def __init__(self, location, housetype, deal_type, price, completion_year): self.location = location self.housetype = housetype self.deal_type = deal_type self.price = price self.completion_year = completion_year def show_detail(self): print(self.location, self.housetype, self.deal_type\ ..

Coding/Python 2021.11.03

[python] ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“  ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ

๊ทธ๋ ‡๋‹ค,, ๊ณ ๋Œ€๋กœ ๋”ฐ๋ผ์ณค๋Š”๋ฐ ์—๋Ÿฌ๋‚˜๋Š” ๋ฏธ์Šคํ…Œ๋ฆฌ,, ์™œ? ์–ธ์  ๊ฐ€ ์ดํ•ดํ•ด์„œ ๋š๋”ฑ๋š๋”ฑ ๊ณ ์น  ์ˆ˜ ์žˆ๋Š” ๋‚  ์˜ค๊ธธ ๋ฐ”๋ผ๋ฉฐ,,,, ์Šคํƒ€๋„ ๋ชจ๋ฅด๊ฒ ๊ณ  ํŒŒ์ด์ฌ๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค~~~~~~~~~ from random import * #์ผ๋ฐ˜์œ  class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed print("{0} ์œ ๋‹›์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(name)) def move(self, location): print("[์ง€์ƒ ์œ ๋‹› ์ด๋™]") print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. [์†๋„ {2}]"\ .format(self.name, location, self.speed)) def d..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(pass, super)

#pass #๊ฑด๋ฌผ class BuildingUnit(Unit): def __init__(self, name, hp, location): pass #์ผ๋‹จ ๋„˜์–ด๊ฐ„๋‹ค. #์„œํ”Œ๋ผ์ด ๋””ํฟ : ๊ฑด๋ฌผ 1๊ฐœ ์žˆ์œผ๋ฉด 8 ์œ ๋‹› ์ƒ์„ฑ supply_depot = BuildingUnit("์„œํ”Œ๋ผ์ด ๋””ํฟ", 500, "7์‹œ") #Another example def game_start(): print("[์•Œ๋ฆผ] ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.") def game_over(): pass ============ Super class Flyable: def __int__(self): print("FlyableFF ์ƒ์„ฑ์ž") class Unit: def __init__(self): print("Unit ์ƒ์„ฑ์ž") class FlyableUni..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

๊ณต๋ถ€ํ•  ๊ฒƒ์€ ๋งŽ๊ณ , ์ดํ•ด๋Š” ์ž˜ ์•ˆ๋˜๊ณ , ์ •๋ฆฌํ•˜๊ธฐ์—” ์–ด๋ ต๊ธฐ์—, ํ•„๊ธฐ๋ฅผ ๊ทธ๋ƒฅ ๋ณต๋ถ™ํ•ฉ๋‹ˆ๋‹ค. #์ผ๋ฐ˜์œ ๋‹› class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed def move(self, location): print("[์ง€์ƒ ์œ ๋‹› ์ด๋™]") print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. [์†๋„ {2}]"\ .format(self.name, location, self.speed)) class AttackUnit(Unit): #์ž์‹ ํด๋ž˜์Šค def __init__(self, name, hp, speed, damage): Unit.__init__(self, name, hp, spe..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(๋‹ค์ค‘์ƒ์†)

#์ผ๋ฐ˜์œ  class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค def __init__(self, name, hp): self.name = name self.hp = hp class AttackUnit(Unit): #์ž์‹ ํด๋ž˜์Šค def __init__(self, name, hp, damage): Unit.__init__(self, name, hp) self.damage = damage def attack(self, location): print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ ๊ตฐ์„ ๊ณต๊ฒฉํ•ฉ๋‹ˆ๋‹ค. [๊ณต๊ฒฉ๋ ฅ {2}]"\ .format(self.name, location, self.damage)) def damaged(self, damage): print("{0} : {1} ๋ฐ๋ฏธ์ง€๋ฅผ ์ž…์—ˆ์Šต๋‹ˆ๋‹ค.".format(self.name, d..

Coding/Python 2021.11.03

211103_Code in the morning

์ ํ”„ํˆฌํŒŒ์ด์ฌ 05์žฅ ์—ฐ์Šต๋ฌธ์ œ Q1 ๋‹ค์Œ์€ Calculator ํด๋ž˜์Šค์ด๋‹ค. class Calculator: def __init__(self): self.value = 0 def add(self, val): self.value += val ์œ„ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” UpgradeCalculator๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ’์„ ๋บ„ ์ˆ˜ ์žˆ๋Š” minus ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. ์ฆ‰ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. cal = UpgradeCalculator() cal.add(10) cal.minus(7) print(cal.value) # 10์—์„œ 7์„ ๋บ€ 3์„ ์ถœ๋ ฅ ํ’€์ด: ๋”๋ณด๊ธฐ class UpgradeCalculator(Calculator): def minus(self, val): self.value -= val cal..

211101_Code in the morning

04์žฅ ์—ฐ์Šต๋ฌธ์ œ Q5 ๋‹ค์Œ์€ "test.txt"๋ผ๋Š” ํŒŒ์ผ์— "Life is too short" ๋ฌธ์ž์—ด์„ ์ €์žฅํ•œ ํ›„ ๋‹ค์‹œ ๊ทธ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. f1 = open("test.txt", 'w') f1.write("Life is too short") f2 = open("test.txt", 'r') print(f2.read()) ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ "Life is too short"๋ผ๋Š” ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋ณด์ž. ํ’€์ด: ๋‹ซ์•„์ค˜์•ผ! ๋”๋ณด๊ธฐ f1 = open("test.txt", 'w') f1.write("Life is too short") f1.close() f2 = open("test.txt", 'r') print(f2.re..

[python] ํด๋ž˜์Šค(ํด๋ž˜์Šค, __init__, ๋ฉค๋ฒ„๋ณ€์ˆ˜)

ํด๋ž˜์Šค, __inti__ class Unit: def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage print("{0} ์œ ๋‹›์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(self.name)) print("์ฒด๋ ฅ {0}, ๊ณต๊ฒฉ๋ ฅ {1}".format(self.hp,self.damage)) marine1 = Unit("๋งˆ๋ฆฐ", 30, 6) marine2 = Unit("๋งˆ๋ฆฐ", 30, 7) ๋ฉค๋ฒ„๋ณ€์ˆ˜ #๋ฉค๋ฒ„๋ณ€์ˆ˜ #์–ด๋–ค ํด๋ž˜์Šค ๋‚ด์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜ #๋ ˆ์ด์Šค : ๊ณต์ค‘ ์œ ๋‹›, ๋น„ํ–‰๊ธฐ, ํด๋กœํ‚น (์ƒ๋Œ€๋ฐฉ์—๊ฒŒ ๋ณด์ด์ง€ ์•Š์Œ) wraith1 = Unit("๋ ˆ์ด์Šค", 80, 5) print("์œ ๋‹› ์ด๋ฆ„ : {0}, ๊ณต๊ฒฉ๋ ฅ : {1}".fo..

Coding/Python 2021.11.01