배운다/파이썬
[파이썬] 삼각형넓이출력 129 : 반복제어문1 - 형성평가5
차차붐
2020. 7. 21. 03:18
문제
삼각형의 밑변의 길이와 높이를 입력받아 출력하고, "Continue? "에서 하나의 문자를 입력받아 그 문자가 'Y' 나 'y'이면 작업을 반복하고 다른 문자이면 종료하는 프로그램을 작성하시오.
(넓이는 반올림하여 소수 첫째자리까지 출력한다.)
입.출력 예
Base = 11
Height = 5
Triangle width = 27.5
Continue? Y
Base = 10
Height = 10
Triangle width = 50.0
Continue? N
풀이 1 )
def triangle(b, h):
area = b * h / 2
return area
while True:
b = input("Base =")
a1 = int (b)
h = input("Height =")
a2 = int (h)
print("Triangle width =", round(triangle(a1, a2),2))
c = input("Continue?: ")
if c.capitalize() == "Y":
continue
else:
break
풀이 2 )
end = False
while not end:
base = float(input("Base = "))
height = float(input("Height = "))
print("Triangle width = ",base * height /2)
if input("Continue? ").lower() != "y":
end = True
풀이 3 )
i = 0
while i == 0:
base = float(input('Base = '))
if base > 0:
height = float(input('Height = '))
else:
i = 1
if height > 0:
print('Triangle width =', round(0.5*base*height, 1))
cont = str(input('Continue? '))
else:
i = 1
if cont != 'y' and cont != 'Y':
i = 1