Inflearn brand logo image

Inflearn Community Q&A

mkim's profile image
mkim

asked

Getting Started with Programming: Introduction to Python (Inflearn Original)

for statement (2-3)

for + else

Written on

·

207

0

 수업 중에 예시로

numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38]

for num in numbers:

if num == 45:

print("Found : 34!")

break

else:

print("45 not found")

를 사용하셨었는데

마지막에 else 없이 그냥 for가 끝나게 하고 프린트만 붙여도 똑같은 값이 나올텐데

굳이 else: 를 써서 좋은 상황이 어떤 게 있을까요?

 

 

 

 

python

Answer 1

0

niceman님의 프로필 이미지
niceman
Instructor

네 안녕하세요.

좋은 질문입니다.

for문에 else 구문이 있다는 것도 설명 드리기 위해 말씀드린 내용이예요!

for문이 정확하게 실행 된다면 이야기 주신것과 같습니다.(for else는 자주 사용하지는 않아요.)

참고 부탁드려요~~

mkim's profile image
mkim

asked

Ask a question