-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTry, Except, and Finally.py
41 lines (34 loc) · 1.14 KB
/
Try, Except, and Finally.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Try, Except, and Finally
# Python will output different types of error notifications. These include:
# - TypeError
# - NameError
# - AttributeError
# - IndentationError
# - SyntaxError
# Python allows us to manage the output error instead of relying on python itself.
# This is where Try, Except, and Finally comes in.
# These clauses can prevent errors from being fatal to our code.
def sum():
try:
v1, v2 = input("Enter two numbers: ").split()
print(int(v1) + int(v2))
except ValueError:
print("Please enter valid numbers as input...!!!")
sum()
sum()
def friends():
animals = ('bird', 'cat', 'dog', 'cow', 'pig', 'horse')
try:
num = int(input('Please select a friend: '))
try:
print(animals[num])
except:
print('You did not choose a friend !!!')
finally:
print('_______________________________')
for pet in animals:
print(pet)
print('_______________________________')
except:
print('You did not enter a valid index !!!')
friends()