Computer Science

Suggest the correction for the error(s) in previous question's code.

Linear Lists

1 Like

Answer

  1. For the case of finding the index of 100 in Lst1 and Lst2, we can use conditional checks to verify if 100 is present in each list before attempting to find its index. If 100 is not found, we print an error message.
  2. For the case of popping elements from Lst2, we use conditional checks to verify if the list is empty before attempting to pop elements. If a list is empty, we print an error message indicating that popping is not possible.

The corrected code is :

Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 originally is: ", Lst1) 
ch = int(input("Enter 1/2/3 and predict which operation was performed? ")) 
if ch == 1:
    Lst1.append(100)
    Lst2.append(100)
elif ch == 2:
    if 100 in Lst1:
        print("Index of 100 in Lst1:", Lst1.index(100))
    else:
        print("Error: 100 is not in Lst1")
    if 100 in Lst2:
        print("Index of 100 in Lst2:", Lst2.index(100))
    else:
        print("Error: 100 is not in Lst2")
elif ch == 3:
    print(Lst1.pop())
    if Lst2:
        Lst2.pop()
    else:
        print("Error: Cannot pop from an empty list (Lst2)")

Answered By

1 Like


Related Questions