在Python中';让我们试试。。。其他的子句,如果try失败,为什么要解析else子句?
我遇到了一个关于try的问题。。。其他的我正在测试是否使用在Python中';让我们试试。。。其他的子句,如果try失败,为什么要解析else子句?,python,try-catch,Python,Try Catch,我遇到了一个关于try的问题。。。其他的我正在测试是否使用try设置了变量。如果没有设置,我只想继续循环。如果变量已经设置,我想运行else部分。然而,Python抛出了一个抖动,因为它试图执行else部分中的操作,但由于变量尚未设置而失败。有点像第二十二条军规?有没有替代方案 代码: test=None 对于num,枚举中的行(数据文件,0): 如果我的字符串在行中: 测试=num 尝试: 测试 除: 通过 其他: if(num-test)尝试使用if语句检查test是否以非NoneType的
try
设置了变量。如果没有设置,我只想继续循环。如果变量已经设置,我想运行else
部分。然而,Python抛出了一个抖动,因为它试图执行else
部分中的操作,但由于变量尚未设置而失败。有点像第二十二条军规?有没有替代方案
代码:
test=None
对于num,枚举中的行(数据文件,0):
如果我的字符串在行中:
测试=num
尝试:
测试
除:
通过
其他:
if(num-test)尝试使用if
语句检查test
是否以非NoneType
的形式存在
test = None
for num, line in enumerate(dataFile, 0):
if myString in line:
test = num
if test is not None:
if (num - test) <= 58:
# do something
尝试使用if
语句检查test
是否以非NoneType
的形式存在
test = None
for num, line in enumerate(dataFile, 0):
if myString in line:
test = num
if test is not None:
if (num - test) <= 58:
# do something
浏览您的代码。。。我将把它简化为:
foo = None
if foo:
print 'Foo is not None' # We never run this
try:
foo # This doesn't do anything, so this segment of the try / except will always end here
except:
print 'The try segment did not raise an error' # We also never get here
else:
print 'Foo is none' # We end up here because Foo is none, so this will print
本质上。。。您的try/except
子句与if/then
语句无关。这是因为你的缩进
因此,在您的示例中,如果mystring不在行中
,那么else
语句中的所有内容都将执行
您可以更轻松地检查未设置为如下所示的变量:
if not foo:
# Do something with foo if it doesn't exist
else:
# Continue running your loop since foo was set
# ...
for num, line in enumerate(dataFile, 0):
# ...
try:
test
except NameError:
# here you say 'skip the rest of loop in case of test was not setted'
continue
# here the rest of the code
浏览您的代码。。。我将把它简化为:
foo = None
if foo:
print 'Foo is not None' # We never run this
try:
foo # This doesn't do anything, so this segment of the try / except will always end here
except:
print 'The try segment did not raise an error' # We also never get here
else:
print 'Foo is none' # We end up here because Foo is none, so this will print
本质上。。。您的try/except
子句与if/then
语句无关。这是因为你的缩进
因此,在您的示例中,如果mystring不在行中
,那么else
语句中的所有内容都将执行
您可以更轻松地检查未设置为如下所示的变量:
if not foo:
# Do something with foo if it doesn't exist
else:
# Continue running your loop since foo was set
# ...
for num, line in enumerate(dataFile, 0):
# ...
try:
test
except NameError:
# here you say 'skip the rest of loop in case of test was not setted'
continue
# here the rest of the code
首先,在代码中不会出现异常,因为测试变量是创建的。由于您从未遇到异常,因此始终会执行else子句(这意味着try/except子句中的else:如果此处未引发异常,则运行这部分代码)
如果您只是想知道是否设置了一个变量,如果它不是继续循环,您可以这样做:
if not foo:
# Do something with foo if it doesn't exist
else:
# Continue running your loop since foo was set
# ...
for num, line in enumerate(dataFile, 0):
# ...
try:
test
except NameError:
# here you say 'skip the rest of loop in case of test was not setted'
continue
# here the rest of the code
在您的情况下,可能更简单的方法是:
for num, line in enumerate(dataFile, 0):
if myString in line:
# in your code, if myString in line, test = num. So, num - test will allways be < 58 when myString in line
# do something
对于num,枚举中的行(数据文件,0):
如果我的字符串在行中:
#在您的代码中,如果myString在line中,test=num。因此,当myString在line中时num-test总是<58
#做点什么
首先,在代码中不会出现异常,因为测试变量已创建。由于您从未遇到异常,因此始终会执行else子句(这意味着try/except子句中的else:如果此处未引发异常,则运行这部分代码)
如果您只是想知道是否设置了一个变量,如果它不是继续循环,您可以这样做:
if not foo:
# Do something with foo if it doesn't exist
else:
# Continue running your loop since foo was set
# ...
for num, line in enumerate(dataFile, 0):
# ...
try:
test
except NameError:
# here you say 'skip the rest of loop in case of test was not setted'
continue
# here the rest of the code
在您的情况下,可能更简单的方法是:
for num, line in enumerate(dataFile, 0):
if myString in line:
# in your code, if myString in line, test = num. So, num - test will allways be < 58 when myString in line
# do something
对于num,枚举中的行(数据文件,0):
如果我的字符串在行中:
#在您的代码中,如果myString在line中,test=num。因此,当myString在line中时num-test总是<58
#做点什么
我无法复制此问题。您能提供一个最小的工作示例吗?或者在本例中,提供一个最小的非工作示例?我可以复制/粘贴一些东西,然后自己体验这个错误?它很容易复制,就像@BrioniusI无法复制这个问题。您能提供一个最小的工作示例吗?或者在本例中,提供一个最小的非工作示例?我可以复制/粘贴一些东西,然后自己体验这个错误?它很容易复制,就像@布里奥尼乌斯