최대 1 분 소요

반복문

for문

 - 반복횟수를 알 때 사용하는 목적

while문

	while 조건식 :
		반복할 문장
  • 조건식이 참이면 반복
  • 반복횟수를 모를 때 사용하는 목적
  • 무한 반복일 경우, 특정 조건에 break를 사용해서 탈출

while Test

이름 10 출력
 cnt = 0
 while cnt != 10 :
   print("{}.한동석".format(cnt))
   cnt += 1

while Task1

qMsg = ("당신의 혈액형은?\n"
        + "1.A형\n2.B형\n3.O형\n4.AB형\n5.나가기\n"
        )
answer_a = "세심하고 거짓말을 잘 못한다."
answer_b = "거침없고 추진력이 좋다."
answer_o = "사교성이 좋다."
answer_ab = "착하다."
errMsg = "다시 입력해주세요."

while True:
    choice = int(input(qMsg))
    result = ""
    
    if choice == 1:
        result = answer_a
    elif choice == 2:
        result = answer_b
    elif choice == 3:
        result = answer_o
    elif choice == 4:
        result = answer_ab
    elif choice == 5:
        break
    else :
        result = errMsg
    
    print(result)

image

while Task2

qMsg = "다음 중 프로그래밍 언어가 아닌 것은?"
choiceMsg = "1.JAVA\n2.파이썬\n3.C언어\n4.망둥어\n"
choice = int(input(qMsg + "\n" + choiceMsg))
answer = 4
result = ""

while result != "정답!":
    if choice == answer:
        result = "정답!"
    elif choice >= 1 and choice <= 4 :
        result = "오답"
    else:
        result = "다시 시도해주세요."
    print(result)

image

댓글남기기