Skip to content
Advertisement

Trying to put error messages if index is out of bound, named removed doesn’t exist, limit adding names to 5 and displaying names individually

student = [] 
opt = 1
while(opt!=0):
    print("1.Add | 2.Edit | 3.Delete | 4:Display | 0:Exit")
    opt = int(input("option:")
    if(opt==1):
        student.append(input("addname:"))
    elif(opt==2):
        edit = int(input("index to edit:"))
        student[edit] = input("Updated name:")
    elif(opt==3):
        student.remove(input("Enter Name:"))
    elif(opt==4):
        for x in range(0,len(student)):
            print(student[(len(student)-1)-x])

so I’m trying to create a code where it will put an error message if index to edit is out of bound, if the named remove doesn’t exist and limiting the add name just up to 5. as well displaying name individual but at my opt==4 it’s displaying them backward and if I removed the negative sign it comes out as an error saying perhaps I have forgotten a comma.

Advertisement

Answer

student = [] 
opt = 1
while(opt!=0):
    print("1.Add | 2.Edit | 3.Delete | 4:Display | 0:Exit")
    opt = int(input("option:")
    if(opt==1):
         if len(student) < 5:
            student.append(input("addname:"))
    elif(opt==2):
        edit = int(input("index to edit:"))
        if edit < len(student):
            student[edit] = input("Updated name:")
        else :
            print ("Index out of bound")
    elif(opt==3):
        student.remove(input("Enter Name:"))
    elif(opt==4):
        for x in range(0,len(student)):
            print(student[x])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement