Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 2: Stock Bookkeeping Write the function transact() as defined below, along

ID: 3903456 • Letter: P

Question

Part 2: Stock Bookkeeping

Write the function transact() as defined below, along with the docstring. This function should operate as the docstring describes. Please note, you need to lookup pricing of a current day and column. Please choose any column you wish inside this function (e.g. “close”). You must not use test_data() to lookup pricing for the current day. This means transact() and test_data() must use a common function that looks up your stock pricing data after it has been loaded, and processed into memory.

Explanation / Answer

def transact(funds,stock,qty,day,buy=False, sell=False):
    if buy and sell:
       print("Error: buy and sell both are true "
       return funds,stock
    file = open("price.txt","r")
    count = 1
    price = 0.0
    for line in file:
        if count == day:
           price = float(line)
           break
        count = count + 1
    totalPrice = qty * price
    if buy:
       if funds < totalPrice:
           print("Error: Insufficient funds "
           return funds,stock
       else:
           funds = funds - totalPrice
           stocks = stocks + qty
           return funds, stocks  
    if sell:
       if stocks < qty:
           print("Error: Insufficient stocks "
           return funds,stock
       else:
           funds = funds + totalPrice
           stocks = stocks - qty
           return funds, stocks       

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote