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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.