AI/Python
[Python] 7-3장 함수 응용(나무, 막대 그래프, 벌집 그리기)
K_EY
2023. 4. 3. 18:09
- n각형 그리기 (터틀 그래픽)
def n_polygon(n, length):
for i in range(n):
t.forward(length)
t.left(360/n)
n_polygon(6, 50) # 6각형
n_polygon(5, 100) # 5각형
- 나무 그리기 (터틀 그래픽)
import random
import turtle
t = turtle.Turtle()
def tree(length):
if length > 5:
t.forward(length)
t.right(20)
tree(length-random.randint(10, 20))
t.left(40)
tree(length-random.randint(10, 20))
t.right(20)
t.backward(length)
t.left(90)
t.color("green")
t.speed(1)
tree(90)
- 막대 그래프 그리기 (터틀 그래픽)
import turtle
t = turtle.Turtle()
t.shape("turtle")
def drawBar(height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(str(height), font=('Times New Roman', 16, 'bold'))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill()
data = [120, 56, 309, 220, 156, 23, 98]
t.color("blue")
t.fillcolor("red")
t.pensize(3)
for d in data:
drawBar(d)
- 터틀 메이즈 러너 (터틀 그래픽)
import random
import turtle
def draw_maze(x, y):
for i in range(2):
t.penup()
if i== 1:
t.goto(x+100, y+100)
else:
t.goto(x, y)
t.pendown()
t.forward(300)
t.right(90)
t.forward(300)
t.left(90)
t.forward(300)
def turn_left():
t.left(10)
t.forward(10)
def turn_right():
t.right(90)
t.forward(10)
t = turtle.Turtle()
t.shape("turtle")
screen = turtle.Screen()
t.speed(0)
draw_maze(-300, 200)
screen.onkey(turn_left, "Left")
screen.onkey(turn_right, "Right")
t.penup()
t.goto(-300, 250)
t.speed(1)
t.pendown()
screen.listen()
screen.mainloop()
- 눈사람 그리기 (터틀 그래픽)
import turtle
import random
t = turtle.Turtle()
t.shape("turtle")
def draw_snowman(x, y):
# 눈사람 1단
t.begin_fill()
t.penup()
t.goto(x, y)
t.pendown()
t.circle(30)
t.end_fill()
# 눈사람 2단
t.begin_fill()
t.penup()
t.right(90)
t.forward(25)
t.left(90)
t.pendown()
t.circle(15)
t.end_fill()
# 눈사람 3단
t.begin_fill()
t.penup()
t.right(90)
t.forward(70)
t.left(90)
t.pendown()
t.circle(40)
t.end_fill()
# 팔 그리기
t.penup()
t.left(90)
t.forward(80)
t.right(90)
t.forward(15)
t.left(30)
t.pendown()
t.forward(30)
t.penup()
t.backward(30)
t.right(30)
t.backward(30)
t.left(130)
t.pendown()
t.forward(30)
s = turtle.Screen()
s.bgcolor('skyblue')
t.fillcolor("white")
for i in range(3):
draw_snowman(random.randint(-70, 70), 0)
t.penup()
t.home()
t.pendown()
- 벌집 그리기 (터틀 그래픽)
import turtle
t = turtle.Turtle()
t.shape("turtle")
def draw_hexa():
for i in range(6):
t.forward(50)
t.left(360/6)
for i in range(6):
t.forward(50)
t.right(60)
draw_hexa()
- 생일 축하 노래
def happyBirthday(name):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday dear", name)
print("Happy Birthday to you!")
happyBirthday("홍길동")
- 수학 문제
def mathQuest(x, y):
ans = int(input("정수 "+str(x)+'+'+str(y)+"의 합은?"))
if ans == x + y:
print("정답입니다.")
else:
print("틀렸습니다.")
x = int(input("첫 번째 정수: "))
y = int(input("두 번째 정수: "))
mathQuest(x, y)
- 원의 면적, 둘레
def circleArea(radius):
global PI
result = PI * radius ** 2
print("반지름이", radius, "인 원의 면적:", result)
def circleCircumference(radius):
global PI
result = PI * 2 * radius
print("반지름이", radius, "인 원의 면적:", result)
PI = 3.141592
circleArea(5)
circleCircumference(5)
※ 두근두근 파이썬 7장 연습문제 참조