filmov
tv
Python 3: Try Except Else Finally - Learn about error handling in Python

Показать описание
Python 3: Try Except Else Finally - Learn about error handling in Python
# Try except
# python cert - important concept
try:
number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))
division = number2 / number1
print(f'Result for division is {division}')
except ZeroDivisionError:
print("Cannot devide by zero!")
except:
print("Something went wrong")
else:
# this block will only run when the try block is successful
print("Division was successful")
finally:
# this block will always run
print("This code will always run")
print("Repeat with better values")
# Try except
# python cert - important concept
try:
number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))
division = number2 / number1
print(f'Result for division is {division}')
except ZeroDivisionError:
print("Cannot devide by zero!")
except:
print("Something went wrong")
else:
# this block will only run when the try block is successful
print("Division was successful")
finally:
# this block will always run
print("This code will always run")
print("Repeat with better values")