gpt4 book ai didi

python - 多个 Try-Excepts 后跟 Python 中的 Else

转载 作者:太空狗 更新时间:2023-10-30 01:40:16 24 4
gpt4 key购买 nike

有没有办法让几个连续的 Try-Except 子句仅在所有子句都成功时才触发一个 Else?

举个例子:

 try:
private.anodization_voltage_meter = Voltmeter(voltage_meter_address.value) #assign voltmeter location
except(visa.VisaIOError): #channel time out
private.logger.warning('Volt Meter is not on or not on this channel')
try:
private.anodization_current_meter = Voltmeter(current_meter_address.value) #assign voltmeter as current meter location
except(visa.VisaIOError): #channel time out
private.logger.warning('Ammeter is not on or not on this channel')
try:
private.sample_thermometer = Voltmeter(sample_thermometer_address.value)#assign voltmeter as thermomter location for sample.
except(visa.VisaIOError): #channel time out
private.logger.warning('Sample Thermometer is not on or not on this channel')
try:
private.heater_thermometer = Voltmeter(heater_thermometer_address.value)#assign voltmeter as thermomter location for heater.
except(visa.VisaIOError): #channel time out
private.logger.warning('Heater Thermometer is not on or not on this channel')
else:
private.logger.info('Meters initialized')

如您所见,如果所有仪表都关闭,您只想打印 meters initialized,但正如目前所写,它仅取决于加热器温度计。有什么方法可以堆叠这些吗?

最佳答案

就我个人而言,我只需要一个行程变量 init_ok 或类似的东西。

将其设置为True,并将所有except子句设置为False,然后在最后测试?

关于python - 多个 Try-Excepts 后跟 Python 中的 Else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2188754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com