군침이 싹 도는 코딩

Python - while loops 본문

Python/Basic

Python - while loops

mugoori 2022. 11. 18. 13:06

 

while 루프는 콜론안에 들어가 있는 식이 True 일때 자신이 포함하는 문장을 실행시킨다.

보통의 while 루프는 초기값을 두고 그 값을 증가시키거나 감소시켜서 True 인지를 체크한다.

 

while 문을 만드는 법

i = 0
while i < 7 :
    print('hello')
    i = i + 1 # << 값을 증감 시키는 식을 넣지 않으면 무한 반복하게된다.

hello
hello
hello
hello
hello
hello
hello

 

 

 

 

while 문을 이용해 무한루프를 만드는 법

 

while True :
    print('hello')

무한루프를 종료하려면 break를 사용하면 된다.

while True :
    sentence=input('문장 입력 : ')
    if sentence == '그만' :
        break
    print(sentence)

 

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

Python - Defaulf parameter  (0) 2022.11.21
Python - function definition  (0) 2022.11.21
Python - range 함수  (0) 2022.11.18
Python - break  (0) 2022.11.18
Python - for 루프 (리스트,딕셔너리 key,value,item)  (0) 2022.11.18