请告诉我它有什么问题。因为当我运行它时它会告诉错误。在线"如果小时> 40"并说它的语法错误!
while true:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
raise ValueError("Non numeric value")
except enter code hereValueError as e:
print (e)
continue
if hrs > 40
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
quit()
答案 0 :(得分:1)
您的代码包含一些语法和语义错误:
true
应以大写字母True
开头。raise
块中使用try
,您创建了一个始终执行的自定义异常,并且永远不会到达if
块。在except
关键字后,您有两个选项:
except ValueError:
print("Non-numeric data found in the file.")
您输入代码的正确方法是:
while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
#raise ValueError("Non numeric value")
except :
print ('Non numeric data found.')
continue
if hrs > 40:
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
quit()
希望它有所帮助!
答案 1 :(得分:0)
你在if statement
的末尾错过了一个冒号。此外,else
不应该是indented
。最后,我纠正了您的try except
声明,因为您无缘无故地提升ValueError
:)。现在,如果输入无法成功转换为ints
,则ValueError
将被引发,loop
将继续(如果没有错误,则代码将继续并{{1}离开循环)。
所以最终的代码是:
break
答案 2 :(得分:0)
我认为这就是你要找的东西:
do_action( 'woocommerce_before_single_product_summary' );
答案 3 :(得分:-1)
你不应该使用加注但只是尝试除了+使用正确的缩进(如果一个else)将永远不会在你的情况下使用(仅在循环时)。试试版本:
while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
except ValueError as e:
print (e)
print("Non numeric value")
continue
if hrs > 40:
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)