尝试除了没有捕获错误

时间:2017-05-01 16:08:24

标签: python python-3.x if-statement try-except

如果输入是非数字的,我试图添加错误消息。我已尝试try / except,现在尝试if / else,但两者都未被激活(例如,如果用户输入" 10%",错误输出与我的错误消息相比)

第一个根据百分比输入计算成绩。第二个应该是计算工资。

grade=eval(input("Enter Score:"))
try:
    if(grade<0 or grade>1):
        print("Bad Score")
    elif(grade>=0.9):
        print("A")
    elif(grade<=0.9 and grade>=0.8):
        print("B")
    elif(grade<=0.8 and grade>=0.7):
        print("C")
    elif(grade<=0.7 and grade>=0.6):
        print("D")
    else:
        print("F")
 except:
     print("Bad score")

Hours=eval(input('Please enter hours worked: '))
Rate=eval(input('Please enter pay per hour: '))
if(Hours<=40 and Hours>=0):
    Pay=Hours*Rate
elif(Hours>40):
    Pay=((Hours-40)*(1.5*Rate))+(40*Rate)
    print('Your pay should be $',Pay)
else:
    print('Error.  Please enter a Numeric Value')

编辑格式化...原始帖子中的代码是正确的,但必须缩进以创建代码灰色框,创建不正确的缩进。

再次感谢你!

1 个答案:

答案 0 :(得分:-1)

糟糕的说法!尝试并期望应该继续自己的路线。像这样:

grade=eval(input("Enter Score:"))
try:
  if(grade<0 or grade>1):
     print("Bad Score")
  elif(grade>=0.9):
     print("A")
  elif(grade<=0.9 and grade>=0.8):
     print("B")
  elif(grade<=0.8 and grade>=0.7):
     print("C")
  elif(grade<=0.7 and grade>=0.6):
     print("D")
  else:
     print("F")
except:
print("Bad score")

Hours=eval(input('Please enter hours worked: '))
Rate=eval(input('Please enter pay per hour: '))
if(Hours<=40 and Hours>=0):
  Pay=Hours*Rate
elif(Hours>40):
  Pay=((Hours-40)*(1.5*Rate))+(40*Rate)
  print('Your pay should be $',Pay)
else:
  print('Error.  Please enter a Numeric Value')