python - 如何使用 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
解决方案
如果要退出程序,请使用exit(code)
而不是。break
推荐阅读
- linux - 无法提交或推送到 github,而且我没有启用 2 因素身份验证
- javascript - Django Bootstrap Modal Ajax - 第一次完美运行,第二次 - 不走运
- css - Tailwind:外接显示器和笔记本电脑屏幕都被认为是 xl 但尺寸差异很大:使导航栏表现不同
- python - 无法使用 python 从 json 响应中获取特定对象
- node.js - Error: Status Code is 403 (MongoDB's 404) 这意味着请求的版本-平台组合不存在
- c++ - 在 C++ 中显示方程
- javascript - 为什么我的“让”变量不清除输入字段?
- mitmproxy - 在服务器重放中忽略 301/2
- amazon-quicksight - 如何在 AWS QuickSight 中使用 runningSum
- html - 在 Google 表格上跟踪和汇总手动修改