Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iTERATION HELP
#1
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()
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Wavic,

Cheers, it works perfectly. 

Regards
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
In the future please use code tags around your code
Thank you
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020