๐Ÿ“˜Python/๐Ÿ‘ฉ‍๐Ÿซclass

๐Ÿ‘ฉ‍๐Ÿซclass : โœจ14week(์‹œํ—˜ ์˜ˆ์ƒ ๋ฌธ์ œ ๋ฐ ์‹ค์Šต ํ•„์š”)

4:Bee 2023. 12. 9. 06:28
728x90

continue ์ด์šฉํ•˜๊ธฐ

for n in range(1,6) :
    if n == 4 :
        #4๋ฅผ ์ œ์™ธํ•˜๊ณ (์ฆ‰,ํŒŒ์‹ฑํ•  ๋•Œ continue๋กœ ์ธํ•ด์„œ ์•„๋ž˜ print๋ฅผ ์ƒ๋žต) ์ถœ
        continue
    print(n, end=' ') #1 2 3 5
print('The End')
for n in range(1,21):
    if n%3 == 0 :
        continue
    print(n,end=' ') #1 2 4 5 7 8 10 11 13 14 16 17 19 20

 

์ด์ค‘ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ 3์˜ ๋ฐฐ์ˆ˜ ๋งŒ๋“ค๊ธฐ

def threeMultiple(n):
    if(n % 3 == 0) :
        return True #boolen์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ 
    else:
        return False

#three = threeMultiple(4)
#print(three)

def threeMuls2(num):
    for n in range(1,num) :
        if threeMultiple(n): #3์˜ ๋ฐฐ์ˆ˜๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด if๋ฌธ ์ฒ˜๋ฆฌ 
            print(n, end = ' ')
    print()
threeMuls2(31)

 

ํ•จ์ˆ˜์˜ ์žฌํ™œ์šฉ์œผ๋กœ ์—ฌ๋Ÿฌ ์›ํ˜• ๋งŒ๋“ค๊ธฐ

import turtle as t

def draw_circle(x,y,radius):
    t.up()
    t.goto(x,y)
    t.down()
    t.circle(radius)

draw_circle(100,50,100)
draw_circle(-100,50,40)
draw_circle(100,-50,30)
draw_circle(-100,-50,70)

onclick์„ ์ด์šฉํ•ด ์›ํ˜• ๊ทธ๋ฆฌ๊ธฐ

import turtle as t

sc = t.Screen()

def draw_circle(x,y):
    t.up()
    t.goto(x,y)
    t.down()
    t.circle(100)

sc.onclick(draw_circle)

sc.mainloop()

ํด๋ฆญํ•œ ์œ„์น˜์— ์›์„ ๊ทธ๋ ค๋‚ธ ๊ฒฐ๊ณผ์ด๋‹ค.

 

onscreenclick์œผ๋กœ ํด๋ฆญํ•œ ์œ„์น˜์— ๋ณ„ ๋งŒ๋“ค๊ธฐ -> forward์™€ goto์˜ ์ฐจ์ด๋ฅผ ์ฐพ์•„๋ณด์ž (event_star.py)

import turtle as turtle
t = turtle.Turtle()
turtle.bgcolor("black")

def star(length):
    for n in range(5):
        t.forward(length) #forward์™€ goto์ฐจ์ด๋Š”?
        t.left(144)

def s_draw(x,y):
    
    t.up()
    t.goto(x,y)
    t.down()
    t.color("yellow")
    t.begin_fill()
    star(50)
    t.end_fill()

scr = turtle.Screen()
scr.onscreenclick(s_draw)

ํด๋ฆญํ•œ ์œ„์น˜์— ๋ณ„์„ ๋งŒ๋“  ๊ฒฐ์ด๋‹ค.

ํ•ด๋‹น ์ฝ”๋“œ์— ์žˆ๋Š” forward์™€ goto์˜ ์ฐจ์ด๋ฅผ ์ƒ๊ฐํ•ด๋ณด๊ณ  ์ฐพ์•„๋ณด์ž.

 

input์œผ๋กœ ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ -> ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ ์‹ค์Šต (evnet_op.py)

#ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ํ†ตํ•ฉํ•ด๋ณด๋ผ 
def add(n1,n2):
    return n1+n2
def subtract(n1,n2):
    return n1-n2
def multiply(n1,n2):
    return n1*n2
def divide(n1,n2):
    return n1/n2

num1 = int(input("์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜ : "))
num2 = int(input("๋‘ ๋ฒˆ์งธ ์ •์ˆ˜ : "))
op = input("์—ฐ์‚ฐ์ž ์ž…๋ ฅ : ")

if op == "+":
    res = add(num1, num2)
elif op == "-":
    res = subtract(num1, num2)
elif op == "*":
    res = multiply(num1, num2)
elif op == "/":
    res = divide(num1, num2)
else : print(op + "๋Š” ์ž˜๋ชป๋œ ์—ฐ์‚ฐ์ž ์ž…๋‹ˆ๋‹ค.")

print(num1, op, num2, '=', res)

ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  UI๋ฅผ ์ง‘์ ‘ ์ œ์ž‘ํ•ด์„œ ๊ณ„์‚ฐ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

728x90