CS 302 Computer Fluency
Elaine Rich

Trust Fund Buddy

 

Our Python book, in Chapter 2, introduces two Trust Fund Buddy programs.  For this project, we are going to make our own versions of those programs.  We are going to make some changes to what the programs actually do.  But, also, to make it easy for you to turn in everything you do in one module, we are going to define callable functions that perform the Trust Fund Buddy actions.

 

Part I

 

The first Trust Fund Buddy program is introduced on page 39. 

 

1.      Start by creating your own module that contains the program trust_fund_bad from the book.  That program has 8 categories of expenses.  Maybe your taste doesn’t run to Lamborghinis.  So change the program so that it uses 8 budget categories that reflect how you would spend the trust fund money if it were yours.

2.      Run the program.  See if you can figure out why it gets the wrong answer.

3.      Convert your code to a callable function named trust_fund_bad.  See below for how to do that.

4.      Call your function to make sure that it works as you expect (namely incorrectly).

 

Part II

 

The second Trust Fund Buddy program is introduced on page 41.

 

1.      In the same module, create a new function called trust_fund_good.  Start with the body of your function using the code from trust_fund_good from the book.  But again there are the old 8 categories of expenses.  So change this second function so that it uses the budget categories you chose for Part I.

2.      Call your function to make sure it works correctly.

 

Part III

 

Your rich grandfather may have set you up with a trust but he’s also given you a mean trustee who is determined to limit your expenses.  He’s asked you for a budget and told you to list 20 budget categories, in descending order of how badly you want them.  But he is going to decide how many of them you’re going to get.  You are guaranteed that he’ll pick a number between 2 and 20.

 

1.      In the same module, create a new function called trust_fund_monthly.  Here’s its outline:

 

def trust_fund_monthly(n):

    """Will let you have your top n ways of spending money."""

 

    # fantasies is a list of money spending opportunities.

    # damages is a matching list of dollar amounts.

 

    fantasies = 20 * [""]       # Before we start to fantasize.

    damages = 20 * [0]          # These just create the lists.

   

 

    # Now fill in the values.

    fantasies[0] = "Extreme sports"

    damages[0] = 10000

    fantasies[1] = "truffles"

    damages[1] = 5000

      

 

    ## fill in 2 through 19

 

 

    ## print a list of the n allowable fantasies

 

 

    ## print the monthly total of the allowable fantasies

 

 

You should fill in the three sections marked with ##.  In other words, you need to make your function do the following three things:

a.       Fill in the lists fantasies and damages.

b.      Print a list of the n allowable fantasies.  (Hint: use a loop.)

c.       Print the monthly total of the allowable fantasies. (Hint: use another loop.)  Make sure to label this number as the monthly total.

 

2.      Call your function to make sure it works correctly.

   

Part IV

 

One month is bad enough.  What about a whole year?

 

1.      In the same module, create a new function called trust_fund_annual.  It should work exactly the same as trust_fund_monthly except:

·         After it prints the list of n allowable fantasies, its next output line should report the annual total (assuming that spending stays the same every month).

·         It should have a final line that reports the difference between your annual spending and that of a small island country Beachland (whose annual spending is $846,678,754).  If your spending is less than that of Beachland, your program should output:

 

You spent <amount> less than Beachland this year.  Congratulations on not wasting money!

 

If your spending is more than that of Beachland, your program should output:

 

You spent <amount> more than Beachland this year.  Congratulations on becoming an island nation!

 

If your spending is exactly equal to that of Beachland, your program should output:

 

You spent <amount> exactly the same amount as Beachland this year.  Congratulations on becoming an island nation!

 

To create this line, you will need to use an if statement, as described starting on p. 53.  Remember that, to check for equality, use == not simply = (which is used only as the assignment operator).  Because you are considering three cases, you will probably want to use elif, which is described starting on p. 59. 

 

Turning in Your Project

 

Use the turnin system to submit a single module with the four functions that you have written.

 

Defining a Function

 

The process of defining functions is described in the book starting on p. 161.  Suppose that you have written the following code:

 

x = 3

y = 6

print(x+y)

 

To turn this code into a function that you can call, you first need to choose a name.  Let’s call our function xy.  Then we write:

 

def xy():

    x = 3

    y = 6

    print(x+y)

 

You can cut and paste your original code to put in your function.  But remember that it must be indented one level.  There are tools to do this automatically, but for short functions, the easiest thing to do is just to insert four blanks at the beginning of every line.