728x90

while문도 for문처럼 반복문입니다.
i = 1
while i <= 10:
print(i)
i += 1

위의 코드는 i가 1이라고 주어져있고 while의 조건이 거짓이 될때까지 반복됩니다.
하지만 파이썬에서는 증감연산자가 없기에 i++를 사용할수 없고 i+=1을 사용했네요.
mysum = 0
mycount = 1
while mycount <= 10:
mysum += mycount
mycount += 1
else:
print('sum', mysum, sep=':')
print("count", mycount, sep=":")

0부터 10까지의 합을 구하는 반복문입니다.
반복문이 정상종료되어야 값이 나오겠네요?
728x90
'Python > Python' 카테고리의 다른 글
[Python] 함수(function) (0) | 2022.05.09 |
---|---|
[Python] break, continue (0) | 2022.05.08 |
[Python] 제어문 - for문 (0) | 2022.05.06 |
[Python] 제어문 - if문 (0) | 2022.05.05 |
[Python] split, join (0) | 2022.05.04 |