python - 如何在“while”循环中结束“try”循环?
问题描述
我在如何结束“尝试”循环时遇到了麻烦,这是因为我有“尝试”而发生的,这里是代码:
import time
class exe_loc:
mem = ''
lib = ''
main = ''
def wizard():
while True:
try:
temp_me = input('Please specify the full directory of the memory, usually it will be a folder called "mem"> ' )
if temp_me is True:
exe_loc.mem = temp_me
time.sleep(1)
else:
print('Error value! Please run this configurator again!')
sys.exit()
temp_lib = input('Please specify the full directory of the library, usually it will be a folder called "lib"> ')
if temp_lib is True:
exe_loc.lib = temp_lib
time.sleep(1)
else:
print('Invalid value! Please run this configurator again!')
sys.exit()
temp_main = input('Please specify the full main executable directory, usually it will be app main directory> ')
if temp_main is True:
exe_loc.main = temp_main
time.sleep(1)
我试着用break
,结束它pass
,我什至把它留空了我得到的是Unexpected EOF while parsing
,我在网上搜索,他们说这是代码块没有完成时造成的。如果我的代码有错误,请告诉我,谢谢。
顺便说一句,我使用的是 python 3,我不知道如何更具体地回答这个问题,如果你不明白,请问我。对不起我糟糕的英语。
编辑:通过删除解决,try
因为我没有使用它,但我仍然想知道如何try
正确结束循环,谢谢。
解决方案
您还必须使用except
语句“捕获”异常,否则尝试没有用。
因此,如果您执行以下操作:
try:
# some code here
except Exception:
# What to do if specified error is encountered
这样,如果您的 try 块中的任何地方引发异常,它不会破坏您的代码,但它会被您的异常捕获。
推荐阅读
- arrays - index(of: not appearing for [[String:Any]]
- google-bigquery - 如何更新 BQ 结构数组中的字段?
- javascript - how can I make this faster. By removing one element from an array, check if array is increasing seuence
- java - AsyncTask 没有足够快地将结果返回给主线程
- android - Android Fragment Menus
- silverstripe - Silverstripe 缓存刷新不会持续存在
- c# - C# WinForms - 图表绘制指定的更多点
- java - intellij,java中未读取命令行参数?
- c# - 随机概率计算不准确
- google-vision - 谷歌视觉 OCR api 没有返回响应