break outside loop

조회수 648회
import random

while True:
    name=input('당신의 이름은 무엇입니까?:')
    print(name,'님','안녕하세요?')

    dice=random.randint(1,6)
    if dice==1:
        print('ㅇ.')
    elif dice==2:
        print('ㄴ.')
    elif dice==3:
        print('ㄹ.')
    elif dice==4:
        print('ㅎ.')
    elif dice==5:
        print('ㄱ.')
    else:
        print('ㅂ.')
print('감사합니다.')

input('시스템을 중지하시겠습니까?')

if answer=='Y'or'y':
    break
elif answer=='N'or'n':
    continue

코드는 이렇게 정했고, answer가 'Y'또는'y'를 눌러야만 break를 하고 싶은데 제목에 써져 있는 오류가 계속 반복이 되어서요. 어느 부분을 어떻게 수정해야 할까요..?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    break나 continue 명령은 루프 안에 존재해야 합니다.

    import random
    
    while True:
        name=input('당신의 이름은 무엇입니까?:')
        print(name,'님','안녕하세요?')
    
        dice=random.randint(1,6)
        if dice==1:
            print('ㅇ.')
        elif dice==2:
            print('ㄴ.')
        elif dice==3:
            print('ㄹ.')
        elif dice==4:
            print('ㅎ.')
        elif dice==5:
            print('ㄱ.')
        else:
            print('ㅂ.')
        print('감사합니다.')
    
        input('시스템을 중지하시겠습니까?')
    
        if answer=='Y'or'y':
            break
        elif answer=='N'or'n':
            continue
    
    • 아아 그렇네요! 감사합니다~ 왕초보 2022.6.11 12:25

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)