Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How many money in 30 days.
#5
You could also do it with a recursive function (a function that calls itself). You know that the day d, the amount will be twice the amount of day d-1, until you reach day 1 where the amount is 0.01. So:

def days(d):
    if d <= 1:
        return 0.01
    else:
        return days(d-1) * 2

while True:
    d = int(input("number of days (0 to stop): "))
    if d == 0:
        break
    else:
        print("Total amount: " + str(days(d)))
The inconvenient of a recursive function is that it is much slower to calculate the result, compared to a direct calculation like the power of 2. The advantage is that is is usually simpler to write and if the formula to go from day d-1 to day d is not simple, you may have some difficulties to find a way to calculate it easily.

As an exercise, consider that you have 50 the first day, that you double each day but you need to subtract 35 at the end of each day for your food. Finding the formula which will give you the result is not so trivial.
Reply


Messages In This Thread
How many money in 30 days. - by miguelramos122 - Dec-16-2017, 11:05 AM
RE: How many money in 30 days. - by j.crater - Dec-16-2017, 11:14 AM
RE: How many money in 30 days. - by zydjohn - Dec-16-2017, 11:51 AM
RE: How many money in 30 days. - by miguelramos122 - Dec-16-2017, 11:52 AM
RE: How many money in 30 days. - by squenson - Dec-16-2017, 12:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How split N days between specified start & end days SriRajesh 2 1,429 May-06-2022, 02:12 PM
Last Post: SriRajesh
Brick How To Sum Numbers Of Next 7 Days Developer_2018 7 2,938 Oct-19-2020, 02:54 PM
Last Post: Developer_2018
  Python code for counting money at multiple stores duddey 9 6,494 Oct-16-2020, 01:17 PM
Last Post: bill_z
  Money conversion - problems with lists and .format function fatherted99 1 1,886 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  MONEY CHANGE OmarSinno 5 5,772 Oct-21-2017, 05:51 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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