@@ -34,26 +34,30 @@ def largest_str(l):
3434def display_reverse (l ):
3535 for i in range (len (l )- 1 , - 1 , - 1 ):
3636 print (l [i ], end = " " )
37+ return
3738
3839# Find a specific item in the list
39- def find_item (item , l ):
40+ def find_item (l ):
41+ item = input ("\n Enter an element: " )
4042 for i in range (0 , len (l ), 1 ):
4143 if item == l [i ]:
4244 print ("Item found at index: " , i )
4345 return
4446 print ("Item not found" )
4547
4648# Remove the specified item in the list
47- def remove_item (item , l ):
49+ def remove_item (l ):
50+ item = input ("\n Enter an element: " )
4851 for i in range (0 , len (l ), 1 ):
4952 if item == l [i ]:
5053 l .remove (item )
5154 print ("Item removed" )
52- return
55+ return
5356
5457# Sort the list in descending order
5558def sort_desc (l ):
56- return sorted (l , reverse = True )
59+ print (sorted (l , reverse = True ))
60+ return
5761
5862# Accept two list and find common members in them
5963def common (l1 , l2 ):
@@ -67,29 +71,52 @@ def common(l1, l2):
6771 else :
6872 print ("No common element" )
6973 return
74+
75+ def main (l ):
76+ print ("\n Menu" )
77+ print ("-" * 20 )
78+ print ("1. Check if all elements are numbers" )
79+ print ("2. Count odd numbers if list is numeric" )
80+ print ("3. Display largest string in list" )
81+ print ("4. Reverse the list" )
82+ print ("5. Find item in list" )
83+ print ("6. Remove item from list" )
84+ print ("7. Sort in Descending order" )
85+ print ("8. Find common elements from another list" )
86+ print ("9. Exit" )
87+ print ("-" * 20 )
88+ option = input ("Your choice: " )
89+ switcher = {
90+ '2' : count_odd ,
91+ '3' : largest_str ,
92+ '4' : display_reverse ,
93+ '5' : find_item ,
94+ '6' : remove_item ,
95+ '7' : sort_desc ,
96+ '8' : common ,
97+ '9' : quit
98+ }
99+ if option == '1' :
100+ if check_int (l ):
101+ print ("All elements are numbers" )
102+ else :
103+ print ("All elements are not numbers" )
104+ elif option == '8' :
105+ l2 = []
106+ n = int (input ("Enter the size of new list: " ))
107+ for i in range (0 , n , 1 ):
108+ l2 .append (input ("Enter element: " ))
109+ common (l , l2 )
110+ else :
111+ func = switcher .get (option , lambda : print ("Invalid Choice!" ))
112+ func (l )
70113
71114if __name__ == "__main__" :
72115 l = []
73116 n = int (input ("Enter the size of list: " ))
74117 for i in range (0 , n ,1 ):
75118 l .append (input ("Enter element: " ))
76-
77- if check_int (l ):
78- print ("All elements are numbers" )
79- else :
80- print ("All elements are not numbers" )
81- count_odd (l )
82- largest_str (l )
83- print ("Reversed list:" , end = " " )
84- display_reverse (l )
85- item = input ("\n Enter an element: " )
86- find_item (item ,l )
87- remove_item (item , l )
88- print ("Sorted in Descending Order: " )
89- print (sort_desc (l ))
90- l2 = []
91- n = int (input ("Enter the size of new list: " ))
92- for i in range (0 , n , 1 ):
93- l2 .append (input ("Enter element: " ))
94- common (l , l2 )
95-
119+ ch = 'y'
120+ while ch .lower () == 'y' :
121+ main (l )
122+ ch = input ("\n Want to continue? [y/n]: " )
0 commit comments