Python Forum

Full Version: Formula function (not too difficult i think)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone! I am new to Python. I want to make a function like the example above. When I type the year number, it should return the result according to the formula above (use return() instead using print(). Thank you for your help Smile .

Year 1 = x
Year 2 = Year 1 *y+x
Year 3 = Year 2*y+x
Year 4 = Year 3*y+x
.
.
.
(continue)


Eg.
Enter Year: 2
Result: xy+x

Eg.
Enter Year: 4
Result: ((xy+x)(y)+x)y+x
not difficult indeed. What have you tried? what is x and what is y?
(Apr-09-2021, 10:43 AM)buran Wrote: [ -> ]not difficult indeed. What have you tried? what is x and what is y?
x = 100 and y = 5

I have tried this, but it seems not working.
def loop(year):

  while 1 < year:
      x = 100 * 5 + 100      
      return x
  return x

if __name__=='__main__': 
  year = 2
  print(loop)
Why are you printing the function on line 10? You need to call the function, passing in the value for its parameter, year.
Also, to keep it general, loop() should have 3 parameters - year, x, and y.
You then do not decrement year so the loop will repeat infinitely.
Finally, you do not need a loop, rather exponentiation (hint, some x and y formula raised to the power of year)
...... Doh Doh Doh
def func(year):
    x = 100; y = 5
    if year == 1: return x
    return (func(year-1))*y+x

for i in range(1, 5):
    print(func(i))
Output:
100 600 3100 15600
Please don't do people's homework for them.