Adding A Money Module To Python

Assignment 4:

This is a sample Python assignment. We can finish the majority assignments within 1 day, but to ensure you have the chance to check through it, it is preferable to contact us with time to spare.

Adding a money module to Python

The task is to program a module called money , which can be used to solve problems involving bank accounts in money.py.

If you use floating point number, you will find they are inaccurate because 0.1 can’t be represented (a number pretty
close to that can be represented, but not exactly 0.1). This leads to rounding errors, for example assuming we have a
a balance of $100 and a monthly interest rate of 0.1% we get the following result.

>>> balance = 100.00
>>> for i in range(12): # once a month for a year
>>> balance += balance * .001 # monthly interest rate
>>> balance
101.20662204957928

This could be rounded as $101.20 or $101.21. but when you have millions of accounts, this can lead to financial
discrepancies. If instead keep the amounts as integers then we have a consistent value, and this is what we will
write in the module money. We keep track of the value as 2 integer values, one of the dollar amount and one
for the cent value. Whenever we perform a calculation we convert the cent value into dollars and cents if necessary.
The amount is stored as a list of length 2 representing dollars and cents inside the money module.

The money module needs to implement:

  1. money (d, c) d = number of dollars, c = number of cents and return value is a list [d, c].
  2. from_string(s) converts a string such as “$19.67” into [19, 57].
  3. string(m) converts the value into a string, so [19, 7] becomes “$19.07” (note the leading 0).

In Python, you can add a leading 0 to a number which is less than 10 as follows:

>>> d = 10
>>> c = 0
>>> '${}.{:02d}'.format(d,c)
'$10.00'

4. add(m1, m2) add 2 money values, if the cents are over 100 then update the cents and dollar amounts.
5. subtract(m1, m2) subtract second value from first. If the cents is negative then update the dollar amount.
6. interest(balance, rate) this MODIFIES the balance, after adding interest.
You may also test your code using the money_test.py file.

If you are looking for a Python tutor or want someone to help with Python project then we can offer a solution.

Money.py

# I have written this for you.
def money(d,c):
    return [d, c]

# this function is passed a string of the for '$d.cc'
# for example '$10.00'.  It should return a list of
# length 2; item 0 in the list is the number of dollars
# (for '$10.00', 10) and item 1 is the number of cents (e.g 0).
def money_from_str(s):
    return money(0,0)  # replace

# this function is passed a list of length 2, representing
# a monetary amount and returns a string of the form '$d.cc'
# You can use the format string '${}.{:02d}'
def string(m):
    return ''     # replace

# extract dollars or cents
# I have written these for you
def dollars(m):
    return m[0]

def cents(m):
    return m[1]

# this might be helpful in other calculations
# although you don't have to use it.
# convert a list of length 2 to an integer
# that represent the total number of pennies that
# are equivalent to the specified monetary amount
# for example all_cents([9,99]) should return 999.
def all_cents(m):
    return 100 * m[0] + m[1]

# add the monetary amounts represented by amt1 and amt2.
# In the result, the cents must be between 0 and 99.
def add(amt1, amt2):
    return money(0,0)   # replace

# subtrat amt2 from amt1
def subtract(amt1, amt2):
    return money(0,0)   # replace

# modify balance to reflect an interest payment
# the 2nd parameter represents an annual interest rate
# interest is credited monthly, so rate should be
# divided by 12.
def interest(balance, rate):
    pass
    # this function is meant to be destructive, so don't
    # return anything

If you need help with your Python proect or are looking for a Python online tutor to help you complete your Python proect then we can offer that for you.

Money_test.py

from money import *

# problem from lab write-up
def test1():
    # we open an account with $100.00
    balance = money(100,0)
    # we don't touch it for a year, and it earns 1.2% interest
    # compounded monthly
    for i in range(12):
        interest(balance,.012)
    return balance

# second problem
# same as first, except opening balance is $500.  This time
# the interest rate is only .6% (annually).  Also, we
# set up an auto-pay for our utility bill, and $30 is deducted
# monthly from our account to pay that.
def test2():
    balance = money(500,0)
    # we don't touch it for a year, and it earns 1.2% interest
    # compounded monthly
    for i in range(12):
        interest(balance,.006)
        balance = subtract(balance, money(30,0))
    return balance

Solution for Assignment 4:

As an example Python program this is the style of help with Python homework we are able to provide.

Money.py

def money(d,c):

    return [d, c]

# this function is passed a string of the for '$d.cc'
# for example '$10.00'.  It should return a list of
# length 2; item 0 in the list is the number of dollars
# (for '$10.00', 10) and item 1 is the number of cents (e.g 0).

def money_from_str(s):

    s = s.replace('$', '')
    dollars, cents = s.split('.')
    dollars = int(dollars)
    cents = int(cents)

    return money(dollars, cents)

# this function is passed a list of length 2, representing
# a monetary amount and returns a string of the form '$d.cc'
# You can use the format string '${}.{:02d}'

def string(m):

    # if the moneny is negative, display it in correct format.

    if m[0] < 0:
        return '-${}.{:02d}'.format(-m[0], m[1])
    else:
        return '${}.{:02d}'.format(m[0], m[1])

# extract dollars or cents
# I have written these for you

def dollars(m):

    return m[0]

def cents(m):

    return m[1]

# this might be helpful in other calculations
# although you don't have to use it.
# convert a list of length 2 to an integer
# that represent the total number of pennies that
# are equivalent to the specified monetary amount
# for example all_cents([9,99]) should return 999.

def all_cents(m):

    return 100 * m[0] + m[1]

# add the monetary amounts represented by amt1 and amt2.
# In the result, the cents must be between 0 and 99.

def add(amt1, amt2):

    amt1 = all_cents(amt1)
    amt2 = all_cents(amt2)
    amt = amt1 + amt2

    # convert cents back to dollar and cent

    return money(amt // 100, amt % 100)

# subtrat amt2 from amt1

def subtract(amt1, amt2):

    amt1 = all_cents(amt1)
    amt2 = all_cents(amt2)
    amt = amt1 - amt2

    # if the result is negative, return only negative in dollars

    if amt < 0:
        amt = -amt
        return money(- (amt // 100), amt % 100)

    return money(amt // 100, amt % 100)

# modify balance to reflect an interest payment
# the 2nd parameter represents an annual interest rate
# interest is credited monthly, so rate should be
# divided by 12.

def interest(balance, rate):

    rate = rate / 12
    cents = all_cents(balance)

    # calculate total cents with interest

    cents = round(cents * (1 + rate))
    cents = int(cents)

    # save the result into the balance array

    balance[0] = cents // 100
    balance[1] = cents % 100

Index:

Contact us for an individual quote for your Python project help.