Python
파이썬 기초 08. while문
Y25N
2023. 9. 18. 16:06
728x90
"""
조건식의 결과가 참인 동안만 반복되고 조건식의 결과가 거짓이 되면 반복문을
빠져나옴
"""
n = 0
while n<5:
n+=1
print(n)
# 1~10까지 더하기
#for
total = 0
for i in range(1,11):
total += i
print(total)
#while
total = 0
n = 1
while n<=10:
total+=n
n+=1
print(total)
#while만 가능한 경우 -> q를 입력할 때까지 반복해서 이름 받기
#break로 반복문 빠져나오기
name = input("name: ")
while name != 'q':
name = input('name: ')
#조건에서 빠져나오기
#break
#while true -> 무한반복
while True:
name = input("name: ")
if name =='q':
break
#연습문제
#올바른 아이디/비밀번호를 입력할 때까지 비밀번호 입력
id = 'id123'
pw = 'pw123'
while True:
inputId = input('id: ')
inputPw = input('pw: ')
if id == inputId and pw == inputPw:
print('Login Success!')
break
#아이디가 잘못되었으면 아이디를 확인하세요
#비밀번호가 잘못되었으면 비밀번호를 확인하세요
id = 'id123'
pw = 'pw123'
while True:
inputId = input('id: ')
inputPw = input('pw: ')
if id != inputId and pw == inputPw:
print('Check Your ID!')
elif id == inputId and pw != inputPw:
print('Check Your PASSWORD!')
elif id != inputId and pw != inputPw:
print('Check Your ID and PASSWORD!')
elif id == inputId and pw == inputPw:
print('Login Success!')
break
#사용자가 0을 입력할 때까지 숫자를 입력받아 입력받은 숫자들의 합을 구하라
sum = 0
total = 0
while True:
userNum = int(input('number: '))
sum += userNum
if userNum == 0 :
print('== finish ==')
total+=sum
print('total: ',total)
break