Skip to content

Instantly share code, notes, and snippets.

@weihanchen
Created September 15, 2023 11:41
Show Gist options
  • Save weihanchen/f2b8bcb9a900cd2a1bf703abbb4aa0bf to your computer and use it in GitHub Desktop.
Save weihanchen/f2b8bcb9a900cd2a1bf703abbb4aa0bf to your computer and use it in GitHub Desktop.
def try_except_else_example():
try:
num1 = int(input("請輸入一個整數: "))
num2 = int(input("請輸入另一個整數: "))
result = num1 / num2
except ZeroDivisionError:
print("錯誤:除以零。")
except ValueError:
print("錯誤:輸入的不是整數。")
else:
print("沒有例外時顯示結果:", result)
try_except_else_example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment