[Tutor] Acting on objects in a list - how to make the change permanent?

Jeff Shannon jeff at ccvcorp.com
Mon Sep 27 23:12:14 CEST 2004


Adam Cripps wrote:

>for i in returned_list:
>    if intchoice == iter_count:
>        action = raw_input ("""Would you like to delete this object?  (y/n)""") 
>        if action == "y":
>            intchoice = intchoice -1 # First one is zero - need to take one off	
>            del returned_list[intchoice]
>            print "deleted"
>            return # Do I need to return the new list?
>    else:
>        print "You chose not to delete"
>        return
>    iter_count = iter_count + 1	# This goes through the loop
>
>  
>

I'm not sure if this is the cause of your problem, but in this code 
you're never incrementing iter_count, and you'll never execute this loop 
for more than one iteration anyhow.   You're returning out of the 
function that this loop is in as soon as you've processed the first 
element, regardless of what you do with it.

Presumably, you want to run this loop over everything in returned_list;  
you can do this simply by removing both return statements.  If, in one 
of the two cases, you want to avoid the iter_count increment at the end 
of the loop, you can use 'continue' to start the next iteration at the 
top of the loop, or 'break' to break out of the for loop but continue 
with the rest of the function that this is in.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list