2017-05-01 146 views
-5

我試圖添加一個錯誤消息,如果輸入是非數字的。我試過try/except現在試圖if/else但都沒有得到激活(例如,如果用戶輸入「百分之十」,有一個錯誤輸出與我的錯誤消息)嘗試除非沒有捕獲錯誤

第一個計算級基於百分比輸入。第二個應該是計算工資。

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

[修復縮進](http://stackoverflow.com/posts/43722313/edit)。你的代碼甚至不會像現在這樣運行。 – khelwood

+1

我想你想要的東西像http://stackoverflow.com/q/23294658/3001761,但也[不要使用裸'除了'](http://blog.codekills.net/2011/09/29/所述-罪惡-的 - 除了 - /)。 – jonrsharpe

+2

@jonrsharpe Nor'eval' .. – DeepSpace

回答

-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')