import math

cumdata = 1
ishere = False

for _ in range(5):
    cumdata *= int(input())
    if math.sqrt(cumdata).is_integer() == True:        
        print('found')
        break
else:
    print('not found')

5개의 값을 입력 받아 차례 대로 곱했을때 제곱수가 있는 경우 found, 없는 경우 not found 를 출력하는 코드이다.

 

보통 flag 라는 변수를 이용해 처리하지만 python의 for-else 문을 이용하여 flag 함수를 제거할 수 있다. 

'Program > Python' 카테고리의 다른 글

가장 많이 나오는 문자열 찾기 (collections의 Counter 함수)  (0) 2022.08.02
itertools의 순열(permutation), 조합(combination)  (0) 2022.08.02
Sinc 함수 구현  (0) 2021.05.06
K번째 큰 수  (0) 2021.05.06
람다 표현식  (0) 2021.04.24

+ Recent posts