DEV Community

sunj
sunj

Posted on

Python While문

while : 특정 조건을 만족할 때, 코드를 반복 실행하는 조건문
조건이 참일 때 = 반복실행
조건이 거짓일 때 = 반복종료

while 조건 : (조건은 한칸 띄어쓰기)
   실행코드 (한블럭 들여쓰기)
Enter fullscreen mode Exit fullscreen mode
print('a가 0보다 같거나 크면 실행, 작으면 정지')

a=int(input('a:'))

while a>= 0:
   print('실행')
   a= int(input('a:'))
Enter fullscreen mode Exit fullscreen mode

무한루프에 빠지는 것을 주의

#n번 반복하기
n= int(input('n:'))

while n: //양수인지 검사
    print(n)
    n=n-1
Enter fullscreen mode Exit fullscreen mode
#1~10까지 반복
n=1
while n<=10:
    print(n)
    n=n+1
Enter fullscreen mode Exit fullscreen mode
text = 'yes'
while text=='yes':
    text=input('yes입력시 반복')

print('종료')
Enter fullscreen mode Exit fullscreen mode
text = input('e 또는 E 입력 시 종료')
while text!='e' and text!='E': //소문자 e가 아니여야 참 대문자 E가 아니여야 참
    text=input('e 또는 E 입력 시 종료')

print('종료')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)