if preset.exists('//*[@id="i0116"]', '10', '1'):
if (log.enter_username()):
if preset.exists('//*[@id="i0116"]', '10', '1'):
if (log.enter_password()):
locked = unlock(details[0], details[1], browser, ' ', ' ', ' ')
if not (locked.checkBlocked()):
# unlock account
else:
searches = search(details[0], details[1], browser)
else:
browser.quit()
continue
else:
browser.quit()
continue
else:
browser.quit()
continue
else:
browser.quit()
continue
我正在用python做一个机器人,我有太多的异常处理要做,我可能最终会得到至少5个以上的if和else…
我在这里做错什么了吗?我应该将这些异常硬编码到方法本身中,而不是使用控制文件来执行吗?
最佳答案
如果你所有其他的陈述都在做同样的事情,那么你最好这样写,
if preset.exists('//*[@id="i0116"]', '10', '1') and log.enter_username() and preset.exists('//*[@id="i0116"]', '10', '1') and log.enter_password():
locked = unlock(details[0], details[1], browser, ' ', ' ', ' ')
if not (locked.checkBlocked()):
# unlock account
else:
searches = search(details[0], details[1], browser)
else:
browser.quit()
continue
关于python - 这是python的良好编码实践吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54066956/