문제
삼각형의 밑변의 길이와 높이를 입력받아 출력하고, "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
'배운다 > 파이썬' 카테고리의 다른 글
[파이썬] 구구단 548 : 반복제어문2 - 자가진단8 (0) | 2020.07.22 |
---|---|
[파이썬] 평균, 패스/페일 546: 반복제어문2 - 자가진단6 (0) | 2020.07.22 |
[파이썬] 나라 수도 출력 633 : 반복제어문1 - 자가진단6 (0) | 2020.07.21 |
[파이썬] 동물 번호 출력 123 : 선택제어문 - 형성평가4 (0) | 2020.07.21 |
[파이썬] 윤년 평년 달력 계산기 122 : 선택제어문 - 형성평가3 (0) | 2020.07.21 |