import turtle colorblue turtle penup turtle goto110 25
사례 연구 : 올릭핌 오륜 로고 그리기 import turtle. color(“blue”) turtle. penup() turtle. goto(-110, -25) turtle. pendown() turtle. circle(45) turtle. color(“red”) turtle. penup() turtle. goto(110, -25) turtle. pendown() turtle. circle(45) turtle. color(“green”) turtle. penup() turtle. goto(55, -75) turtle. pendown() turtle. circle(45) turtle. color(“black”) turtle. penup() turtle. goto(0, -25) turtle. pendown() turtle. circle(45) turtle. color(“yellow”) turtle. penup() turtle. goto(-55, -75) turtle. pendown() turtle. circle(45) turtle. done() 2017년 하계 KS 프로그래밍 교육
사례 연구: 생일 알아맞히기 • 생일 알아맞히기는 쉽게 해결할 수 있는 흥미로운 프로그램이다 = 19 + 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 Set 2 Set 3 Set 4 16 20 24 28 17 21 25 29 18 22 26 30 19 19 23 27 31 Set 5 2017년 하계 KS 프로그래밍 교육
사례 연구: 뺄셈 프로그램 - 소스 import random # 1. 한 자리 정수를 생성한다. number 1 = random. randint(0, 9) number 2 = random. randint(0, 9) # 2. number 1 < number 2이면 두 수를 교환한다. if number 1 < number 2: number 1, number 2 = number 2, number 1 # 동시 할당문 # 3. "number 1 - number 2은/는 얼마입니까? "라고 묻는다. answer = eval(input(str(number 1) + " - " + str(number 2) + "은/는 얼마입니까? ")) # 4. 답을 검사하고 결과를 출력한다. if number 1 - number 2 == answer: print("정답입니다!") else: print("틀렸습니다. n", number 1 , "-", number 2, "은/는", number 1 - number 2, "입니다. ") 2017년 하계 KS 프로그래밍 교육
중첩 if 문과 다중 if-else 문 if score >= 90. 0: grade = 'A' else: if score >= 80. 0: grade = 'B' else: if score >= 70. 0: grade = 'C' else: if score >= 60. 0: grade = 'D' else: grade = 'F' 동일 (a) if score >= 90. 0: grade = 'A' elif score >= 80. 0: grade = 'B' elif score >= 70. 0: grade = 'C' elif score >= 60. 0: grade = 'D' else: grade = 'F' (b) 더 낫다 2017년 하계 KS 프로그래밍 교육
실행 흐름도 score >= 90 True grade = 'A' False score >= 80 True grade = 'B' False score >= 70 True grade = 'C' False score >= 60 False True grade = 'D' grade = 'F' 2017년 하계 KS 프로그래밍 교육
if-else문 트레이스 가정: score는 70. 0 조건이 False if score >= 90. 0: grade = 'A' elif score >= 80. 0: grade = 'B' elif score >= 70. 0: grade = 'C' elif score >= 60. 0: grade = 'D' else: grade = 'F' 2017년 하계 KS 프로그래밍 교육
if-else문 트레이스 - cont 가정: score는 70. 0 if score >= 90. 0: 조건이 False grade = 'A' elif score >= 80. 0: grade = 'B' elif score >= 70. 0: grade = 'C' elif score >= 60. 0: grade = 'D' else: grade = 'F' 2017년 하계 KS 프로그래밍 교육
if-else문 트레이스 - cont 가정: score는 70. 0 if score >= 90. 0: grade = 'A' elif score >= 80. 0: grade = 'B' 조건이 True elif score >= 70. 0: grade = 'C' elif score >= 60. 0: grade = 'D' else: grade = 'F' 2017년 하계 KS 프로그래밍 교육
if-else문 트레이스 - cont 가정: score는 70. 0 if score >= 90. 0: grade = 'A' elif score >= 80. 0: grade = 'B' grade는 C elif score >= 70. 0: grade = 'C' elif score >= 60. 0: grade = 'D' else: grade = 'F' 2017년 하계 KS 프로그래밍 교육
사례 연구: 체질량 지수 계산하기 - cont # 사용자로부터 파운드 단위의 몸무게를 입력받는다. weight = eval(input("몸무게(파운드)를 입력하세요: ")) # 사용자로부터 인치 단위의 키를 입력받는다. height = eval(input("키(인치)를 입력하세요: ")) KILOGRAMS_PER_POUND = 0. 45359237 # 상수 METERS_PER_INCH = 0. 0254 # 상수 # BMI를 계산한다. weight. In. Kilograms = weight * KILOGRAMS_PER_POUND height. In. Meters = height * METERS_PER_INCH bmi = weight. In. Kilograms / (height. In. Meters * height. In. Meters) # 결과를 출력한다. print("BMI는", format(bmi, ". 2 f"), "입니다. ") if bmi < 18. 5: print("저체중") elif bmi < 25: print("정상") elif bmi < 30: print("과체중") else: print("비만") 2017년 하계 KS 프로그래밍 교육
not 연산자의 진리표 p not p 예제 (age=24, gender=’F’) True False not (age > 18)은 False이다. False True not (gender == ‘M’)은 True이다. 2017년 하계 KS 프로그래밍 교육
and 연산자의 진리표 p 1 p 2 p 1 and p 2 False False True True 예제(가정: age=24, gender=’F’) (age > 18) and (gender == ‘F’)는 True (age > 18) and (gender != ‘F’)는 False 2017년 하계 KS 프로그래밍 교육
or 연산자의 진리표 p 1 p 2 p 1 or p 2 False True False True 예제(가정: age=24, gender=’F’) (age > 34) or (gender == ‘F’)는 True (age > 34) or (gender == ‘M’)는 False 2017년 하계 KS 프로그래밍 교육
range(a, b) Python Shell >>> for v in range(4, 8): . . . print(v) . . . 4 5 6 7 >>> 2017년 하계 KS 프로그래밍 교육
range(b) Python Shell >>> for i in range(4): . . . print(i) . . . 0 1 2 3 >>> 2017년 하계 KS 프로그래밍 교육
range(a, b, step) Python Shell >>> for v in range(3, 9, 2): . . . print(v) . . . 3 5 7 >>> 2017년 하계 KS 프로그래밍 교육
break sum = 0 number = 0 while number < 20: number += 1 sum += number 루프부터 빠져 나온다 if sum >= 100: break print("마지막 숫자는", number, "입니다. ") print("합계는", sum, "입니다. ") 2017년 하계 KS 프로그래밍 교육
continue sum = 0 number = 0 while (number < 20): number += 1 반복의 맨 끝으로 건너뛴다 if (number == 10 or number == 11): continue sum += number print("The sum is ", sum) 2017년 하계 KS 프로그래밍 교육
랜덤워크 - 소스 import turtle from random import randint turtle. speed(1) # turtle을 최저속도로 설정한다. # 16 X 16 격자를 그린다. turtle. color("gray") # 격자의 색상 x = -80 for y in range(-80, 80 + 1, 10): turtle. penup() turtle. goto(x, y) # 가로선을 그린다. turtle. pendown() turtle. forward(160) y = 80 turtle. right(90) for x in range(-80, 80 + 1, 10): turtle. penup() turtle. goto(x, y) # 세로선을 그린다. turtle. pendown() turtle. forward(160) turtle. pensize(3) turtle. color("red") turtle. penup() turtle. goto(0, 0) # 중앙으로 이동한다. turtle. pendown() x = y = 0 # 격자의 중앙에서 현재 pen의 위치 while abs(x) < 80 and abs(y) < 80: r = randint(0, 3) if r == 0: x += 10 # 오른쪽으로 걷는다. turtle. setheading(0) turtle. forward(10) elif r == 1: y -= 10 # 아래쪽으로 걷는다. turtle. setheading(270) turtle. forward(10) elif r == 2: x -= 10 # 왼쪽으로 걷는다. turtle. setheading(180) turtle. forward(10) elif r == 3: y += 10 # 뒤쪽으로 걷는다. turtle. setheading(90) turtle. forward(10) turtle. done() 2017년 하계 KS 프로그래밍 교육
Reference • 파이썬, Y. Daniel Liang 저, 길준민, 정재화 역, 생능출판 • Introduction to Programming Using Python, Y. Daniel Liang – 저자 사이트 : http: //www. cs. armstrong. edu/liang/py/ 2017년 하계 KS 프로그래밍 교육
- Slides: 52