首页 > 解决方案 > 如何使用 break 命令退出代码?

问题描述

请有人给我一些关于如何break在python中使用命令的帮助。我想在有人进入时终止程序'quit'

我已经放置'quit'在下面的input()循环中,所以当'quit'输入程序终止时 - 但认为它只是退出输入循环并崩溃其余代码 - 显然我没有按照预期的方式使用它。

while True:
    user_input = input(prompt).lower()
    if user_input in ('apples', 'pears', 'oranges', 'quit'):
        # the user = int(0),int(1), int(2) values just assign a different column numnber
        if user_input == 'apples':
            col_number = 0
        if user_input == 'pears':
            col_number = 1
        if user_input == 'oranges':
            col_number = 2
        if user_input == 'quit':
            break
        return col_number, user_input

my_col, my_input = get_input(prompt='Enter apples, pears, oranges or q to quit')

错误信息:

{'Yellow', 'Blue', 'Green'}
Enter apples, pears, oranges or q to quitquit
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/MatPlotLib/open_multiple_files_8.py", line 60, in <module>
    my_col, my_input = get_input(prompt='Enter apples, pears, oranges or q to quit')
TypeError: cannot unpack non-iterable NoneType object

Process finished with exit code 1

标签: python

解决方案


如果要退出程序,请使用exit(code)而不是。break


推荐阅读