好吧,我是python的新手,我正在学校上课,我对此感到困惑。我们正在编写一个程序/脚本来计算买卖股票的交易,由于某种原因,我无法获得随时间累积的“余额”变量,并从买卖股票中扣除并增加。这是代码。任何输入都会很棒^。^
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()
答案 0 :(得分:7)
您打印差异balance - transaction_amount
,但您从未将此值设置为balance
变量。差不多,你做的事情与:
>>> cupcakes = 5
>>> print("I ate one cupcake, now I have", cupcakes - 1, "cupcakes")
I ate one cupcake, now I have 4 cupcakes
>>> print(cupcakes)
5 # We never modified the variable cupcakes
在打印报表之前,请执行balance = balance - transaction_amount
(或balance -= transaction_amount
),然后在打印功能中添加balance
。
答案 1 :(得分:1)
transaction_amount_buy = transaction_amount_buy + (num_shares * price_per_share)
balance = balance - transaction_amount_buy
print("Currently holding", buy, "and have $", balance , "in cash")
这应该让第一部分工作,现在其余部分应该遵循......
答案 2 :(得分:1)
我建议您将此问题解决为一系列较小的问题。通过创建仅处理较小问题的函数来做到这一点。在尝试将它们全部连接在一起之前,让每个函数都有效。
示例:无论您是否购买了东西,都有余额。 您选择购买或出售。你买什么或卖什么?如果你卖的话,你拥有它吗?如果没有,那可以吗?是不同的卖(卖空)你不拥有的东西。
所以保持简单,开始吧。假设您只购买或出售香蕉。购买100种不同于销售100种香蕉的香蕉?如果您处理的是大于0的数字并且卖出的数字小于零,则不是真的。
所以你有(现金)余额。当您“购买”时,您将退还成本。余额应按成本递减。如果你卖掉,余额应该增加成本。
嗯..你能卖掉你不拥有的东西吗?您似乎不仅需要跟踪您的余额,还需要跟踪您购买或出售的商品中的“头寸”。如果您的销售量超过您自己的价格,或者购买价格超过您的余额的香蕉,您需要有办法处理这些问题。