【发布时间】:2017-01-13 09:45:39
【问题描述】:
我希望用户为程序提供输入,这样如果用户输入错误,代码应该提示他们输入正确的值。
我尝试了这段代码,但由于continue 语句,它从一开始就运行循环。我希望代码返回到其各自的 try 块。请帮忙。
def boiler():
while True:
try:
capacity =float(input("Capacity of Boiler:"))
except:
print ("Enter correct value!!")
continue
try:
steam_temp =float(input("Operating Steam Temperature:"))
except:
print ("Enter correct value!!")
continue
try:
steam_pre =float(input("Operating Steam Pressure:"))
except:
print ("Enter correct value!!")
continue
try:
enthalpy =float(input("Enthalpy:"))
except:
print ("Enter correct value!!")
continue
else:
break
boiler()
【问题讨论】:
-
在每个
try语句周围放一段时间?此外break仅适用于最后一次尝试捕获。
标签: python exception-handling try-catch