Получение ошибки при использовании команды break в Python - PullRequest
0 голосов
/ 20 июня 2020

Итак, я пытаюсь использовать разрыв в своем коде для выбора выхода из программы, но когда я запускаю его, я получаю «разрыв за пределами L oop». Есть способ исправить? Я пробовал команду возврата или другие команды из других вопросов, похожих на этот, но мне не повезло с ними. Вот фрагмент кода, о котором я говорю:

v1 = []
v2 = []

while True:

print('Welcome to vector arithmetic! Below is a list of available operations.')

print('1. Enter values for your vectors')
print('2. Print vectors')
print('3. Vector dimensionality')
print('4. Add vectors')
print('5. Compute dot product of vectors')
print('6. Compute magnitude of vectors')
print('7. Quit')

choice = int(input('Enter the number corresponding to your choice: '))

if choice == 7:
    break
elif choice == 1:
    pass
elif choice == 2:
    pass
elif choice == 3:
    pass
elif choice == 4:
    pass
elif choice == 5:
    pass
elif choice == 6:
    pass
else:
    print('Invalid choice, try again')

Ответы [ 3 ]

1 голос
/ 20 июня 2020

В python имеют значение отступ и пробелы. Как упоминается в приведенном выше комментарии, я просто переместил весь этот блок под вашим оператором while в правильный отступ и работает так, как вы ожидаете.

v1 = []
v2 = []

while True:
    print('Welcome to vector arithmetic! Below is a list of available operations.')
    print('1. Enter values for your vectors')
    print('2. Print vectors')
    print('3. Vector dimensionality')
    print('4. Add vectors')
    print('5. Compute dot product of vectors')
    print('6. Compute magnitude of vectors')
    print('7. Quit')

    choice = int(input('Enter the number corresponding to your choice: '))

    if choice == 7:
        break
    elif choice == 1:
        pass
    elif choice == 2:
        pass
    elif choice == 3:
        pass
    elif choice == 4:
        pass
    elif choice == 5:
        pass
    elif choice == 6:
        pass
    else:
        print('Invalid choice, try again')

1 голос
/ 20 июня 2020

Мне кажется, что у вас ошибка отступа , в этом случае вам нужно убедиться, что при вводе чего-либо в функции l oop, или class , что он имеет правильный отступ.

v1 = []
v2 = []

while True:

    print('Welcome to vector arithmetic! Below is a list of available operations.')

    print('1. Enter values for your vectors')
    print('2. Print vectors')
    print('3. Vector dimensionality')
    print('4. Add vectors')
    print('5. Compute dot product of vectors')
    print('6. Compute magnitude of vectors')
    print('7. Quit')

    choice = int(input('Enter the number corresponding to your choice: '))

    if choice == 7:
        break
    elif choice == 1:
        pass
    elif choice == 2:
        pass
    elif choice == 3:
        pass
    elif choice == 4:
        pass
    elif choice == 5:
        pass
    elif choice == 6:
        pass
    else:
        print('Invalid choice, try again')

Кроме того, я не рекомендую вам использовать break в циклах, поскольку они могут быть проблемными c, если они не реализованы должным образом. Вместо этого попробуйте использовать quit

1 голос
/ 20 июня 2020

Ваш код имеет неправильный отступ:

v1 = []
v2 = []

while True:

    print('Welcome to vector arithmetic! Below is a list of available operations.')

    print('1. Enter values for your vectors')
    print('2. Print vectors')
    print('3. Vector dimensionality')
    print('4. Add vectors')
    print('5. Compute dot product of vectors')
    print('6. Compute magnitude of vectors')
    print('7. Quit')

    choice = int(input('Enter the number corresponding to your choice: '))

    if choice == 7:
        break
    elif choice == 1:
        pass
    elif choice == 2:
        pass
    elif choice == 3:
        pass
    elif choice == 4:
        pass
    elif choice == 5:
        pass
    elif choice == 6:
        pass
    else:
        print('Invalid choice, try again')

Как видите, отступ в python очень важен.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...