프로그램
[파이썬] 문제 : turtle(터틀) 입력 받은 2개의 2진수로 논리합 계산해서 도장찍기
오디세이99
2023. 3. 29. 18:01
728x90
반응형
import turtle
import random
turtle.shape("turtle")
# a = '1111011'
# b = '111001000'
a = input('2진수를 입력하세요 : ')
b = input('2진수를 입력하세요 : ')
c = bin(int(a,2) | int(b,2)) # 논리합 계산
turtle.right(-90) # turcle이 위를 바라보도록 함
for cnt in range(3): # 입력 a, b와 결과 c. 3개
if cnt == 0: # cnt에 따라 a,b,c_str를 지정
dd = a
elif cnt == 1:
dd = b
elif cnt == 2:
dd = str(c)[2:] # 논리합 결과를 문자열로 변환. '0b'는 뺌
if cnt == 0: # a,b,c 에 따라 줄바꿈 하기 위함 세로축 y 계산
y = 0
else:
y = -((cnt) * 60) # 위에서 아래로 가기 때문에 - 임.
for i in range(len(dd)): # a,b,c 에서 각각의 자리수 길이만큼 반복
if dd[i] == '1': # 숫자가 1이면 빨강색, 크기 2(크게)
turtle.shapesize(2)
turtle.color('red')
elif dd[i] == '0': # 숫자가 0이면 파란색. 크기 1(작게)
turtle.shapesize(1)
turtle.color('blue')
turtle.stamp() # turtle 도장 찍기
turtle.penup() # 각 문자 이동하기 위해 펜 up
turtle.goto(-(40 * i), y) # 각 문제 이동하기 위한 가로축(x) 계산. 오른쩍 정렬하이 위해 -1 함.
turtle.done()
728x90
반응형