如何使用中断功能退出代码Python

问题描述 投票:-1回答:2

请有人可以在python中使用'break'命令给我一些帮助。我想在有人输入“退出”时终止程序。我在下面的输入循环中放置了“退出”,因此当输入“退出”时,程序将终止-但认为它只是退出了输入循环并导致了其余代码的崩溃-显然,它没有按照预期的方式使用。任何赞赏。

    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')
错误味精

{''黄色','蓝色','绿色'}输入苹果,梨,橘子或q退出追溯(最近一次通话):文件“ C:/用户/用户/PycharmProjects/MatPlotLib/open_multiple_files_8.py”,第60行,在my_col,my_input = get_input(提示=“输入苹果,梨,橙子或q退出”)TypeError:无法解压缩不可迭代的NoneType对象

退出代码为1的处理完成

python loops break
2个回答
0
投票

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


0
投票

要使您的程序退出执行,我会使用:

import sys

sys.exit()
© www.soinside.com 2019 - 2024. All rights reserved.