#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    JES beginner Need urgent help in JES functions


    I need help with the following functions in my first university assignment, i would appreciate any help please, im so lost in programming
    If i can get help to start this assignment i would be so grateful.

    assignment:
    The following five functions allow you to maintain the running balance of an account and print out lines relating to each transaction.

    You'll also need a global variable (balance?) to maintain the running balance of the account.

    def setBalance(amt): # Defines (but doesn't print) the value of the account balance

    def printBalance():
    # Displays current balance as a money value with a heading

    def printLedgerLine(date, amount, details): # with items (and the balance) spaced and formatted

    def deposit (date, details, amount): # Alter the balance and print ledger line

    def withdraw(date, details, amount): # Alter the balance and print ledger line
    Your task is to:

    complete the five (very short) functions by creating the code for the body of each function, and
    demonstrate that you code works by calling the functions, as is demonstrated below.
    So when these functions are called
    e.g.

    setBalance(500)
    printBalance()
    withdraw("17-12-2012", "BP - petrol", 72.50)
    withdraw("19-12-2012", "Countdown", 55.50)
    withdraw("20-12-2012", "munchies", 1.99)
    withdraw("22-12-2012", "Vodafone", 20)
    deposit ("23-12-2012", "Income", 225)
    withdraw("24-12-2012", "Presents", 99.02)
    printBalance()


    This is how much i could do but it doesnt seem to work(printBalance doesnt print amount balance entered in setBalance(amount))
    def setBalance(amount):
    global balance

    def printBalance(amount)
    if amount > 0:
    printNow(amount)

    def withdrawal(x):
    global balance
    balance = setBalance(amount) - withdrawal(x)
    return balance

    if setBalance(amount) > withdrawal(x):
    printNow(balance)
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by kidoman
    This is how much i could do but it doesnt seem to work(printBalance doesnt print amount balance entered in setBalance(amount))
    Normal, as each function has its local scope. You have to use a global variable for the amount (inside the printBalance body) or much better than that, define the whole set of functions inside a class.

    Using global variable, the code would be something similar to this
    Code:
    g_balance = 0
    
    
    def setBalance(p_amount):
        global g_balance
        if p_amount:
            g_balance = p_amount
    
    
    def printBalance():
        global g_balance
        if g_balance:
            print g_balance
          
          
    def main():
        setBalance(500)
        printBalance()
        
        
    main()
    Using a class the same code will be something similar to this

    Code:
    class AccountBalance:
        
        def __init__(self):
            self.__balance = 0
                
                
        def setBalance(self, p_amount):
            if p_amount:
                self.__balance = p_amount
                
                
        def printBalance(self):
            if self.__balance:
                print(self.__balance)
                
                
    def main():
        lo_balance = AccountBalance()
        lo_balance.setBalance(200)
        lo_balance.printBalance()
    
    
    main()

    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by dariyoosh
    Normal, as each function has its local scope. You have to use a global variable for the amount (inside the printBalance body) or much better than that, define the whole set of functions inside a class.

    Using global variable, the code would be something similar to this
    Code:
    g_balance = 0
    
    
    def setBalance(p_amount):
        global g_balance
        if p_amount:
            g_balance = p_amount
    
    
    def printBalance():
        global g_balance
        if g_balance:
            print g_balance
          
          
    def main():
        setBalance(500)
        printBalance()
        
        
    main()
    Using a class the same code will be something similar to this

    Code:
    class AccountBalance:
        
        def __init__(self):
            self.__balance = 0
                
                
        def setBalance(self, p_amount):
            if p_amount:
                self.__balance = p_amount
                
                
        def printBalance(self):
            if self.__balance:
                print(self.__balance)
                
                
    def main():
        lo_balance = AccountBalance()
        lo_balance.setBalance(200)
        lo_balance.printBalance()
    
    
    main()

    Regards,
    Dariyoosh
    Thank you so much for the help, i have done the following code with your help:
    g_balance = 0


    def setBalance(amount):
    global g_balance
    if amount:
    g_balance = amount


    def printBalance():
    global g_balance
    if g_balance:
    printNow("%s $%0.2f" % ("Currrent balance is",g_balance))


    I have no idea on how to do the deposit and withdraw functions and i dont even know what a ledger line is...i have holidays now so theres no lectures to help me with this
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by kidoman
    I have no idea on how to do the deposit and withdraw functions and i dont even know what a ledger line is...i have holidays now so theres no lectures to help me with this
    So why not Google a bit in order to find the meaning of these terms before trying to write a program for subjects that you don't understand?


    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo