I have to create a list with a menu options for
1. Add,
2. Remove,
3. Check if element exists,
4.Display list,
5. would you like to Continue?
6. Exit
I could write the code so far. Pls help me complete it. (ASAP)

fruits = ['Apple', 'Banana', 'Grapes', 'Peach']
#while 1:

ch = int(raw_input('1. Append \n2. Remove \n3. Exists \n4. Display \n5. Exit \nEnter Your Choice'))

if (ch == 1):
    f_app = raw_input('Enter the fruit to be added')
    fruits.append(f_app)
    print fruits

elif (ch == 2):
    print fruits
    f_rem = raw_input('Enter the fruit to be removed')
    fruits.remove(f_rem)
    print fruits

elif (ch == 3):
    f_exists = raw_input('Enter the fruit to be checked')
    if f_exists in fruits:
        f_in = fruits.index(f_exists)
        print '"%s" exists in the list at index "%s"' %f_exists%f_in
    else:
        print '"%s" does not exist' %f_exists

elif (ch == 4):
    print fruits

elif (ch == 5):
    print ('Exiting!!!')

else:
    print 'Invalid Choice'

Recommended Answers

All 2 Replies

Only strange thing I see is print line in exist case, which has twice % instead of tuple:

print( '"%s" exists in the list at index "%s"' % (f_exists, f_in))

Just enable the while loop, fix exit, fix little output format and you are done.

Can you correct the above code and post it?Pls..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.