개발공부

[Python] While 반복문 본문

Python/Basic

[Python] While 반복문

mscha 2022. 4. 25. 17:58

While

 - While loop 는, 조건식을 만족하는 동안, 자신이 포함하는 문장들을 실행한다.

 

예제 - hello를 7번 출력하기

>>> i = 0 

>>> while i < 7 :
>>>     print('hello')
>>>     i = i + 1

hello
hello
hello
hello
hello
hello
hello

 

Infinite Loop : 무한루프

 - 반복문이 무한히 지속되는 상태를 말한다.

 - break를 이용해 빠져나올 수 있다.

 

예제 - '그만' 이라고 입력할때까지 문자를 입력받고 출력하기

>>> while True :
>>>     user_input = input('문장을 입력하세요 : ')
>>>     if user_input == '그만' :
>>>         break
>>>     else :
>>>         print(user_input)
 

문장을 입력하세요 : 가
가
문장을 입력하세요 : 나
나
문장을 입력하세요 : 다
다
문장을 입력하세요 : 그만