코드예시👨🏻‍💻▶️ 파이썬

파이썬 터틀 그래픽 (turtle graphics) 랜덤한 색의 N각형 그리기

코뮤니티 2020. 10. 21. 17:24

질문 내용

파이썬 터틀을 이용하여 원하는 다각형을 그리고, 각각의 다각형 색을 랜덤으로 주어 출력하고 싶습니다.

 

 

질문 풀이

import turtle
import random
    
t=turtle.Turtle()

def draw_polygon(n, length, x):
    color=["blue", "coral", "green", "purple", "yellow"]
    
    for i in range(x):
        for i in range(n):
            t.fd(length)
            t.lt(360/n)
            #for 문 내에서 각 변의 색 설정
            t.color(color[random.randint(0,4)])
        t.lt(360/x)

n = int(input("원하는 다각형의 종류를 입력하시오"))
length = int(input("한변의 길이를 입력하시오"))
x=int(input("반복할 횟수를 입력하시오"))

draw_polygon(n, length, x)
t.done()

 

코드 결과


 

 

나와 어울리는 개발자 유형 찾기

MBTI로 알아보는 개발자 유형 내 안의 개발자를 찾아서...⭐

comu.codeuniv.kr