-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise7.txt
86 lines (77 loc) · 3.54 KB
/
Exercise7.txt
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
1) Write a program that creates a dictionary like this
{
1: "red" , 2: "Blue" , 3: "Orange"
}
Now take the key as input from the user and print its corresponding colour
(Exception handle the code to terminate gracefully by printing
Colour not found if the key doesnot exists )
2) Write a program that creates a list of 5 elements of your choice
Now take the index that the user want the data of and print the value at that
location
Exception handle the code to terminate gracefully by printing
Value not found if the index doesnot exists
3) Create program that takes age of the user as input
and prints number of days that user has lived for
Exception handle the code such that if the user has lived for more than
100001 days then user should get the message
, you lived for so long , may be you will die soon:)
4) Complete the bits and pieces of the following below code
def my_exception_store():
my_int_list1=[]
my_int_list2=[]
my_het_list3=[]
while(True):
try:
print("Welcome to my_exception_store !!!!")
print("-------------------------------------")
print("Following operations are supported :")
print("1) Create a positive numbered list ")
print("2) Create a negative numbered list ")
print("3) Create a heterogenous list ")
print("4) Check if the element is present in the list ")
print("5) Refresh the program to start with blank lists")
print("6) Exit ")
choice = int(input("Please enter your choice !!!! "))
if choice ==1:
create_positive_numbered_list(my_int_list1)
elif choice ==2:
create_negative_numbered_list(my_int_list2)
elif choice ==3:
create_heterogenous_list(my_het_list3)
elif choice ==4:
print("Lists created are as below \n ----------------------")
print(f"1 : {my_int_list1}")
print(f"2 : {my_int_list2}")
print(f"3 : {my_het_list3}")
while True:
check =int(input("Which of the below list you would want to search from "))
if check == 1:
find_an_element(my_int_list1)
break
elif check == 2:
find_an_element(my_int_list2)
break
elif check ==3:
find_an_element(my_het_list3)
break
else:
print("Please choose something from the above")
elif choice ==5:
my_int_list1.clear()
my_int_list2.clear()
my_het_list3.clear()
print("Store restarted !!!! ")
elif choice ==6:
break
else:
print("Please choose something from the above")
except negative_number_error:
print("We received a negative number in the list and I handled negative_number_error exception")
my_int_list1.clear()
except positive_number_error:
print("We received a positive number in the list and I handled positive_number_error exception")
my_int_list2.clear()
except homogenous_list_error:
print("We received a Homogenous list and I handled homogenous_list_error exception")
my_het_list3.clear()
my_exception_store()