break outside loop

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

import random

while True:
    name=input('What is your name?:')
    print(name 'sir', 'hello?')

    dice=random.randint(1,6)
    if dice==1:
        print ('.'.')
    elif dice==2:
        print('n.')
    elif dice==3:
        print('.'.')
    elif dice==4:
        print('h.')
    elif dice==5:
        print ('a')
    else:
        print('b.')
print ('Thank you.')

input('Do you want to stop the system?')

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

The code is set like this, and I want to break only when answer presses 'Y' or 'y', but the error written in the title keeps repeating. Which part should I modify?

python

2022-09-20 08:56

1 Answers

The break or continue command must exist in the loop.

import random

while True:
    name=input('What is your name?:')
    print(name 'sir', 'hello?')

    dice=random.randint(1,6)
    if dice==1:
        print ('.'.')
    elif dice==2:
        print('n.')
    elif dice==3:
        print('.'.')
    elif dice==4:
        print('h.')
    elif dice==5:
        print ('a')
    else:
        print('b.')
    print ('Thank you.')

    input('Do you want to stop the system?')

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


2022-09-20 08:56

If you have any answers or tips


© 2022 pinfo. All rights reserved.