APCSP Create Task Program Code
APCSP Create Task Program Code
def easyreadmoney(price):
stringprice = str(round(price,2))
appendafterperiod = ""
periodtime = 0
for i in range(len(stringprice)):
if stringprice[i] == ".":
periodtime = 1
if periodtime == 1:
appendafterperiod = (stringprice[i:])
stringprice = stringprice[:i]
break
counter = 0
if len(appendafterperiod) == 2:
appendafterperiod = appendafterperiod + "0"
elif len(appendafterperiod) > 3:
appendafterperiod = str(round(int(appendafterperiod[1:]), 2))
for i in range(len(stringprice), 0, -1):
if counter%3 == 0 and counter != 0:
stringprice = stringprice[:i]+ "," + stringprice[i:]
counter += 1
stringprice = stringprice+(appendafterperiod)
return stringprice
purchase_history = []
def buy_stock(stocknumber, quarterly_val, account_balance):
quarterly_val -= 1
numberbought = input("How many shares (whole number) of
"+stocknamelist[stocknumber]+" would you like to buy?\n")
if is_integer(numberbought) == 1:
if numberbought == "0":
print("You cannot buy zero shares!")
return 0
numberbought = int(numberbought)
stocklist = list_of_stockprices[stocknumber]
pricebought = stocklist[quarterly_val]*numberbought
if pricebought <= account_balance:
print("Buying "+ str(numberbought)+" shares of " +
stocknamelist[stocknumber]+" costs $"+str(easyreadmoney(pricebought))+". Your account
balance will drop from $"+str(easyreadmoney(account_balance))+" to
$"+str(easyreadmoney(account_balance-pricebought))+"." )
confirmbuy = input("Type Y if you would like to buy this. If not, enter
anything else to cancel.\n" )
if confirmbuy == "Y" or confirmbuy == "y":
account_balance -= pricebought
print("You now have $"+str(easyreadmoney(account_balance))+" in your
account.")
purchase_history.append([stocknumber, stocklist[quarterly_val],
numberbought, stocklist[quarterly_val]*numberbought, quarterly_val])
else:
print("Purchase canceled.")
else:
print("You don't have enough money to buy that! Your balance would be
negative $"+(easyreadmoney((account_balance-pricebought)*-1))+".")
return(account_balance)
def numberofshares(stocknumber):
totalshares = 0
for i in purchase_history:
if i[0] == stocknumber:
totalshares += i[2]
return totalshares
def stockboughtinfo(stocknumber):
numberofpurchases = 0
for purchase in purchase_history:
if purchase[0] == stocknumber:
print("You bought "+str(purchase[2])+" shares in quarter " +
str(purchase[4])+ " for $"+str(purchase[1])+" per share, for a total of
"+str(purchase[3])+".")
else:
numberofpurchases += 1
if numberofpurchases == len(purchase_history):
print("You have not bought any shares of "+stocknamelist[stocknumber]+".")
return(1)
return
checkquarters = 0
while checkquarters == 0:
quarter = input("How many quarters (each quarter has the option to buy or sell
stocks) should there be? Select an integer from 3 to 30: \n")
checkquarters = is_integer(quarter)
if checkquarters == 1:
if int(quarter) < 3 or int(quarter) > 30:
checkquarters = 0
print("Out of range!")
quarter = int(quarter)
for i in range(len(list_of_stockprices)):
specific_change_range = listofranges[i]
setstockprices(list_of_stockprices[i],
random.uniform(specific_change_range[0]+0.05, specific_change_range[1]+0.07),
[specific_change_range[0], specific_change_range[1]])