[英]How can I end a 'try' loop in 'while' loop?
I'm in trouble about how to end a 'try' loop, which is occurred since I have the 'try', here is the code: 我在如何结束“ try”循环时遇到麻烦,这是由于我拥有“ 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)
I tried end it by using break
, pass
, and I even leaves it empty what I get is Unexpected EOF while parsing
, I searched online and they said it is caused when the code blocks were not completed. 我尝试通过使用break
, pass
结束它,甚至Unexpected EOF while parsing
我得到的都是Unexpected EOF while parsing
,我在线进行搜索,他们说这是代码块未完成时引起的。 Please show me if any of my code is wrong, thanks. 如果我的代码有误,请告诉我,谢谢。
Btw, I'm using python 3 and I don't know how to be more specific for this question, kindly ask me if you did not understand. 顺便说一句,我正在使用python 3,我不知道该问题的具体方式,请问我是否不明白。 Sorry for my poor english. 对不起,我英语不好。
EDIT: Solved by removing the try
because I'm not using it, but I still wanna know how to end a try
loop properly, thanks. 编辑:通过删除try
解决了问题,因为我没有使用它,但是我仍然想知道如何正确结束try
循环,谢谢。
You have to also 'catch' the exception with the except
statement, otherwise the try has no use. 您还必须使用except
语句“捕获”异常,否则尝试没有用。
So if you do something like: 因此,如果您执行以下操作:
try:
# some code here
except Exception:
# What to do if specified error is encountered
This way if anywhere in your try block an exception is raised it will not break your code, but it will be catched by your except. 这样,如果在您的try块中的任何地方引发了异常,它都不会破坏您的代码,但是它将被您的except捕获。
Your problem isn't the break
, it's the overall, high-level shape of your try
clause. 您的问题不是break
,而是try
子句的整体,高级形状。
A try
requires either an except
or a finally
block. try
需要except
或finally
块。 You have neither, which means your try
clause is never actually complete. 您都没有,这意味着您的try
子句实际上从未完成。 So python keeps looking for the next bit until it reaches EOF (End Of File), at which point it complains. 因此,python一直在寻找下一位直到到达EOF(文件结束)为止,此时它抱怨。
The python docs explain in more detail, but basically you need either: python文档更详细地解释了,但是基本上您需要:
try:
do_stuff_here()
finally:
do_cleanup_here() # always runs, even if the above raises an exception
or 要么
try:
do_stuff_here()
except SomeException:
handle_exception_here() # if do_stuff_here raised a SomeException
(You can also have both the except
and finally
.) If you don't need either the cleanup or the exception handling, that's even easier: just get rid of the try
altogether, and have the block go directly under that while True
. (你还可以兼得的except
和finally
)。如果你不需要任何清理或异常处理,这是更容易:刚刚摆脱的try
完全,并有块直接去那下while True
。
Finally, as a terminology thing: try
is not a loop. 最后,作为术语, try
不是循环。 A loop is a bit of code that gets executed multiple times -- it loops. 循环是多次执行的少量代码-循环。 The try
gets executed once. try
执行一次。 It's a "clause," not a "loop." 这是一个“条款”,而不是一个“循环”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.