在 Python 中使用 break 命令时出错

氪金

所以我试图在我的代码中使用中断来选择退出程序,但是当我运行它时,我得到“中断外循环”。有办法解决吗?我已经尝试过类似这个问题的其他问题中的 return 命令或其他命令,但我没有运气。这是我所指的一段代码:

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 中,缩进和空格很重要。正如上面的评论所提到的,我只是将 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')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章