except,它返回excepts,但继续运行,python hours=输入(“输入小时:”) 速率=输入(“输入速率:”) 尝试: 小时=浮动(小时) 利率=浮动(利率) 除: 打印(“输入实数”) def computepay(小时、费率): 如果是40小时: 工资=40*费率 exhrs=(小时-40)*(1.5*费率) totpay=支付+支出 全额返还 打印(“付款:%s”%computepay(小时,费率)) [1] : 这是正常的行为。若您引发一些异常,那个么脚本将被终止,在其他情况下,except将只打印消息。except不终止脚本。
当except,它返回excepts,但继续运行,python hours=输入(“输入小时:”) 速率=输入(“输入速率:”) 尝试: 小时=浮动(小时) 利率=浮动(利率) 除: 打印(“输入实数”) def computepay(小时、费率): 如果是40小时: 工资=40*费率 exhrs=(小时-40)*(1.5*费率) totpay=支付+支出 全额返还 打印(“付款:%s”%computepay(小时,费率)) [1] : 这是正常的行为。若您引发一些异常,那个么脚本将被终止,在其他情况下,except将只打印消息。except不终止脚本。,python,except,Python,Except,当try块中的代码引发异常时,执行except块。没有魔法可以让你知道你的用户需要重新输入这些数字,你必须自己提供代码 此外,您应该始终对例外情况进行详细说明。一种可能的方法是 hours = input("Enter hours: ") rate = input("Enter rate: ") try: hours = float(hours) rate = float(rate) except: print("Enter real numbers") def c
try
块中的代码引发异常时,执行except
块。没有魔法可以让你知道你的用户需要重新输入这些数字,你必须自己提供代码
此外,您应该始终对例外情况进行详细说明。一种可能的方法是
hours = input("Enter hours: ")
rate = input("Enter rate: ")
try:
hours = float(hours)
rate = float(rate)
except:
print("Enter real numbers")
def computepay(hours, rate):
if hours <= 40.0:
pay = hours * rate
return pay
elif hours > 40:
pay = 40 * rate
exhrs = (hours - 40) * (1.5 * rate)
totpay = pay + exhrs
return totpay
print("Pay: %s" % computepay(hours, rate))
你说它继续运行是什么意思?
while True:
hours = input("Enter hours: ")
rate = input("Enter rate: ")
try:
hours = float(hours)
rate = float(rate)
break # end the while loop if no error occurred
except ValueError:
print("Enter real numbers")