2015-12-21 101 views
-5
try: 
    f1=int(input("enter first digit")) 
    f2=int(input("enter second digit")) 

    answ=(f1/f2) 
    print (answ) 
except ZeroDivisionError: 
+1

我沒有看到在這裏downvotes的理由。我認爲這個特定的語法錯誤是神祕的,因爲它可能是由許多原因造成的。只需查看[documentation](https://docs.python.org/2/tutorial/errors.html#handling-exceptions)來處理異常,這可能是不夠的。雖然在那裏提到了「SomeError:pass除外」的語法,但沒有明確指出塊不能留空,這對於初學者來說可能會令人費解。 – Reti43

回答

1

你不能有一個except線後面沒有任何東西。你必須有一些代碼,即使它沒有做任何事情。

try: 
    f1=int(input("enter first digit")) 
    f2=int(input("enter second digit")) 

    answ=(f1/f2) 
    print (answ) 
except ZeroDivisionError: 
    pass 
+0

非常感謝你 – hunk

+0

@hunk'pass'用於這種情況,你必須把某些東西放在一個塊中,但是你想要稍後實現它,或者把它留空。 – Reti43

0

在Python中,當您編寫':'時,您將啓動一個代碼塊。而且,按照語法,您不能將塊保留爲空。

所以,你必須完成block.e.g。

try : 
    f1=int(input("enter first digit")) 
    f2=int(input("enter second digit")) 
    answ=(f1/f2) 
    print (answ) 
except ZeroDivisionError:: 
    print ("You can't divide by zero") 
+1

非常感謝你 – hunk

+0

第6行的語法錯誤。** :: **應該替換爲**:** –