我对如何使用 Try/Exception
和 if/else
感到困惑。如果我想告诉用户提供.html
文件
if url[-4:] ==".html":
// do your things
else:
print('Error! the file is not html file')
我正在检查我是否应该在这种情况下使用 try/exception
或像我那样使用 if/else
。
最佳答案
简而言之:
try:
a = q.get()
try
表示尝试这个东西,如果有效,就使用它,else except
如果它失败了,或者它有效,并且出现诸如 ValueError
。
except:
a = None
更新:
try:
url[-4:] == ".html"
except:
print "Error"
关于python - Try/except 检查字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537780/