Hi Guys,

Please be gentle with me as im a complete n00b. My favorite language is actually Java which im learning at Uni at the moment. However to throw a spanner in the works they have switched us over to Python, as well as learning Java. My mind has been completely boggled. I just need to understand this for the moment and Im hoping someone out there will be kind enough to help me. I have created a list with names in, and i want to delete what ever name i wish to do so. So i have put a for loop in there. Now I can delete any name WITHOUT the if-statement in there but when i put if statements in there so set either condition to be true or false it doesnt do what i want it to. here is my code:

Any ideas? Thanks :-)

   names=(['Paul','Tim','Fred'])
   def main():
       for i in range(len(names)):
       e=names[i]
           print names
           print
           a = raw_input("Choose name to be deleted:\n")
       if(a==e)
           names.remove(a)
           print "name deleted"
       if(a!=e):
           print "No such name!Please Re-enter!"

Recommended Answers

All 2 Replies

For starters, you have declared names to be a tuple containing one list so you would access it with
e=names[i][0]. But anyway, your code cleaned up a bit.

names=['Paul','Tim','Fred']
for nm in names:
    print nm

while True:  ## infinite loop until break is called
    a = raw_input("Choose name to be deleted:\n")
    if a in names:
        names.remove(a)
        print "name deleted"
        break
    else:
        print "No such name!Please Re-enter!"

Wow, and it's not even April first yet.

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.