I am having trouble with a list that doesn't want to append like they normally do. I have been messing around with it for the last couple of days off and on. For the life of me I can't seem to figure out what I am doing wrong.

n=range(0,31)
x=[]
for x in n:
AL=5000.00
MM=[]
MM_Income=12000.00
MM_Cost=8000.00
MM_Inc=800.00
MM_Own=11
MM_ROI=(MM_Income/(MM_Cost+(MM_Inc*x)+AL)*100)
MM.append(MM_ROI)
print (MM_ROI)
MML=zip(n,MM)
print (MML)

Rather than giving me a list, I am only getting the last figure.
Any help or ideas would be greatly appreciated. Oh yeah, I already tried using .extend instead of .append and python threw up some errors.

Recommended Answers

All 3 Replies

Add 2 other print statements to see what is happening. Note that the statement
x=[]
does nothing since you redefine x in the next statement.

n=range(0,31)
x=[]
for x in n:
   AL=5000.00
   MM=[]
   print "start of loop", MM
   MM_Income=12000.00
   MM_Cost=8000.00
   MM_Inc=800.00
   MM_Own=11
   MM_ROI=(MM_Income/(MM_Cost+(MM_Inc*x)+AL)*100)
   MM.append(MM_ROI)
   print "after appending", MM
   print (MM_ROI)
   MML=zip(n,MM)
   print (MML)

I have had the print commands you suggested in the code, but they were just telling me what I already knew. Rather than appending the list MM, it is replacing the item each time. I have the x=[] because it is used earlier in another part of the script, so I am clearing the value.

The forum untabified my code. It should have been like this:

n=range(0,31)
x=[]
for x in n:
   AL=5000.00
   MM=[]
   MM_Income=12000.00
   MM_Cost=8000.00
   MM_Inc=800.00
   MM_Own=11
   MM_ROI=(MM_Income/(MM_Cost+(MM_Inc*x)+AL)*100)
   MM.append(MM_ROI)
   print "after appending", MM
   print (MM_ROI)
MML=zip(n,MM)
print (MML)

The key is these lines
MM=[]
print "start of loop", MM
If it is just overlaying the previous item, then this print should print the previous item. What exactly does the first line above do each time you cycle through the for loop?

Edit: try this instead

n=range(0,31)
x=[]
MM=[]
for x in n:
   print "\nreally at the start of loop", MM
   AL=5000.00
   MM=[]
   print "start of loop", MM
   MM_Income=12000.00
   MM_Cost=8000.00
   MM_Inc=800.00
   MM_Own=11
   MM_ROI=(MM_Income/(MM_Cost+(MM_Inc*x)+AL)*100)
   MM.append(MM_ROI)
   print "after appending", MM
   print (MM_ROI)

MML=zip(n,MM)
print (MML) 

print "x is", x, type(x)
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.