Python Forum
Formula function (not too difficult i think) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Formula function (not too difficult i think) (/thread-33246.html)



Formula function (not too difficult i think) - Jam_XD02 - Apr-09-2021

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


RE: Formula function (not too difficult i think) - buran - Apr-09-2021

not difficult indeed. What have you tried? what is x and what is y?


RE: Formula function (not too difficult i think) - Jam_XD02 - Apr-09-2021

(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)



RE: Formula function (not too difficult i think) - ndc85430 - Apr-10-2021

Why are you printing the function on line 10? You need to call the function, passing in the value for its parameter, year.


RE: Formula function (not too difficult i think) - jefsummers - Apr-10-2021

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)


RE: Formula function (not too difficult i think) - naughtyCat - Aug-27-2021

...... 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



RE: Formula function (not too difficult i think) - ndc85430 - Aug-28-2021

Please don't do people's homework for them.