请有人给我一些有关如何在python中使用break
命令的帮助。我想在有人输入'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
答案 0 :(得分:0)
如果要退出程序,请使用exit(code)
代替break
。
答案 1 :(得分:0)
要使您的程序退出执行,我将使用:
import sys
sys.exit()
答案 2 :(得分:0)
由于我没有足够的声誉来发表评论,因此问题似乎是您在使用break语句时没有返回任何内容,而分配期望有两个值。
所以也许代替
if user_input == 'quit':
break
使用
if user_input == 'quit':
col_number = None
并在输入后添加支票