C-log

👩‍🏫class : ✨14week(시험 예상 문제 및 실습 필요) 본문

📘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

'📘Python > 👩‍🏫class' 카테고리의 다른 글

👩‍🏫class : ✨15week  (0) 2023.12.09
👩‍🏫class : 13week  (1) 2023.12.09
👩‍🏫class : 12week  (0) 2023.12.08
👩‍🏫class : 10week  (1) 2023.12.08
👩‍🏫class : 중간고사  (0) 2023.12.08
Comments