954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Perform all methods and operations on a list

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'
sravanthi89
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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

sravanthi89
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: