Python Forum
Formula function (not too difficult i think)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formula function (not too difficult i think)
#1
Star 
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
Reply
#2
not difficult indeed. What have you tried? what is x and what is y?
Underscore likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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)
buran write Apr-09-2021, 11:00 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#4
Why are you printing the function on line 10? You need to call the function, passing in the value for its parameter, year.
Reply
#5
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)
Reply
#6
...... 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
Reply
#7
Please don't do people's homework for them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculating of difficult integrals in python enitorc 0 5,491 Apr-15-2018, 01:24 PM
Last Post: enitorc

Forum Jump:

User Panel Messages

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