Python Forum

Full Version: iTERATION HELP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
 Although I'm newbie in Python I am trying to do an iteration for a calculator than I built in Excel.
Below you can see the function created. Basically, I want to iterate for 30 cycles where cycle one = cycle 0 * number, but cycle 2= cycle one * number where number is a constant. I know it works with the code below but if I want to do 1000 cycles it will force me to write 1000 times the same calculation. I tried to find a solution but I haven't got a clear answer. 
Can you give me hand? I know there must be an easier way because, otherwise programming makes no sense!!!
Cheers 
def OPEX_evolution():

   Total_initial = Total_OPEX
   print(1,Total_initial)

   for n in range(2,3):
       Total_annually_1= Total_initial*(1+Interest_Rate)
       print(n,Total_annually_1)
   for n in range(3,4):
       Total_annually_2=Total_annually_1*(1+Interest_Rate)
       print(n,Total_annually_2)
   for n in range(4,5):
       Total_annually_3=Total_annually_2*(1+Interest_Rate)
       print(n,Total_annually_3)
   for n in range(5,6):
       Total_annually_4=Total_annually_3*(1+Interest_Rate)
       print(n,Total_annually_4)
   for n in range(6,7):
       Total_annually_5=Total_annually_4*(1+Interest_Rate)
       print(n,Total_annually_5)
   for n in range(7,8):
       Total_annually_6=Total_annually_5*(1+Interest_Rate)
       print(n,Total_annually_6)
   for n in range(8,9):
       Total_annually_7=Total_annually_6*(1+Interest_Rate)
       print(n,Total_annually_7)
   for n in range(9,10):
       Total_annually_8=Total_annually_7*(1+Interest_Rate)
       print(n,Total_annually_8)

OPEX_evolution()
Hello! The for loops are not loops. Put print(n) in the first for loop to see what it would print out. range(2,3) evaluate to [2]. Same for all other range functions.

Try this:

def OPEX_evolution(r):
    Total_initial = Total_OPEX
    print(1,Total_initial)
    Total_initial_h = Total_initial

    for n in range(2, r):
        Total_initial_h = Total_initial_h * (1 + Interest_Rate)
        print(Total_initial_h)

OPEX_evolution(1000)
'r' can be 10, 15 or 1000.
Wavic,

Cheers, it works perfectly. 

Regards
Good!
Next time use the code tags when you paste a code. The fifth button from the right in the full edit

I have used additional variable Total_initial_h just to keep the original one unchanged. But if you don't need it you can use it in the loop in place of Total_initial_h
In the future please use code tags around your code
Thank you