Python , vairiable doesnt accumlate over time?
Okay well i'm new to python, and im taking a class in school for this, and
im a bit confused this. We are writing a program/script to calculate
transactions from buying and selling shares. and for some reason i cant
get the "balance" variable to accumulate over time and be subtracted from
and added to from buying and selling the shares. Here's the code, any
input would be amazing ^.^
def main():
#Below is the variables used in the context
balance = float(input("Starting cash amount? "))
numtrades = int(input("Number of trades for today?"))
print('You do not own any shares, but have', balance, 'in cash')
buy = 0
sell = 0
price_per_share = 0
transaction_amount = 0
transaction_amount_buy = 0
transaction_amount_sell = 0
#Below is the prompt for the first transaction, which requires a buy
num_shares = int(input('Number of shares to buy?'))
price_per_share = float(input('Price per share?'))
print(num_shares, "shares for $",price_per_share, "per share cost
$",price_per_share * num_shares)
buy = buy + num_shares
transaction_amount_buy = transaction_amount_buy + (num_shares *
price_per_share)
print("Currently holding", buy, "and have $", balance -
transaction_amount_buy , "in cash")
if balance < transaction_amount :
print('You do not have sufficient funds to purchase', num_shares,
'for $', price_per_share, 'per share.')
print('Your current balance is', balance, ', but', num_shares,' x
', price_per_share,' = ', num_shares * price_per_share)
print("Currently holding", buy, "and have $", balance -
transaction_amount , "in cash")
#Below is the loop counter for each trade, along with if statements for
buy/sell
for i in range(numtrades):
print('Trade number', (i + 2), end = "")
action = input(' (buy/sell)?')
if action == 'buy':
num_shares = int(input('Number of shares to buy?'))
if action == 'sell':
num_shares = int(input('Number of shares to sell?'))
price_per_share = float(input('Price per share?'))
print('Transaction', (i+2))
print('Transaction type is', action)
if action == 'buy':
print(num_shares, "shares for $",price_per_share, "per share cost
$",price_per_share * num_shares)
buy = buy + num_shares
transaction_amount_buy = transaction_amount_buy + (num_shares *
price_per_share)
if action == 'sell':
print(num_shares, 'shares for $',price_per_share, 'per share worth
$',price_per_share * num_shares)
sell = sell + num_shares
transaction_amount_sell = transaction_amount_sell + (num_shares *
price_per_share)
transaction_amount = transaction_amount_buy - transaction_amount_sell
if balance < transaction_amount :
print('You do not have sufficient funds to purchase', num_shares,
'for $', price_per_share, 'per share.')
print('Your current balance is', balance - transaction_amount, ',
but', num_shares,' x ', price_per_share,' = ', num_shares *
price_per_share)
print("Currently holding", buy, "and have $", balance -
transaction_amount , "in cash")
if balance > transaction_amount :
print("Currently holding", buy, "and have $", balance -
transaction_amount , "in cash")
main()
No comments:
Post a Comment