Python 变量不';你不会随着时间积累吗?

Python 变量不';你不会随着时间积累吗?,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,好吧,我是python新手,我正在学校上这门课,对此我有点困惑。我们正在编写一个程序/脚本来计算买卖股票的交易,由于某种原因,我无法让“余额”变量随时间累积,并从买卖股票中减去或添加。这是密码。任何输入都将是惊人的^ def main(): #Below is the variables used in the context balance = float(input("Starting cash amount? ")) numtrades = int(input(

好吧,我是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()
def main():
#下面是上下文中使用的变量
余额=浮动(输入(“起始现金金额?”)
numtrades=int(输入(“今天的交易数量?”)
打印('您不拥有任何股份,但有',余额,'现金')
购买=0
卖出=0
每股价格=0
交易金额=0
交易\金额\购买=0
交易记录\金额\销售=0
#下面是第一笔交易的提示,需要购买
num_shares=int(输入('要购买的股份数?'))
每股价格=浮动(输入(“每股价格”)
打印(num_shares,“shares for$”,price_per_shares,“per shares cost$”,price_per_shares*num_shares)
买入=买入+股数
交易金额购买=交易金额购买+(股数*每股价格)
打印(“当前持有”,买入,“持有美元”,余额-交易金额,买入,“现金”)
如果余额<交易金额:
打印('您没有足够的资金购买',num_股份,'美元',每股价格,'每股')
打印('您当前的余额是',余额',但是',num_shares,'x',price_per_shares,'=',num_shares*price_per_shares)
打印(“当前持有”,买入,“持有美元”,余额-交易金额,“现金”)
#下面是每笔交易的循环计数器,以及买卖的if语句
对于范围内的i(numtrades):
打印('Trade number',(i+2),end=“”)
动作=输入(‘(买入/卖出)’)
如果操作==‘购买’:
num_shares=int(输入('要购买的股份数?'))
如果操作=='sell':
num_shares=int(输入(“要出售的股份数量”)
每股价格=浮动(输入(“每股价格”)
打印('交易',(i+2))
打印('交易类型为',操作)
如果操作==‘购买’:
打印(num_shares,“shares for$”,price_per_shares,“per shares cost$”,price_per_shares*num_shares)
买入=买入+股数
交易金额购买=交易金额购买+(股数*每股价格)
如果操作=='sell':
打印(num_shares,'shares for$',price_per_shares,'per shares worth$',price_per_shares*num_shares)
卖出=卖出+股数
交易金额卖出=交易金额卖出+(股数*每股价格)
交易金额=交易金额购买-交易金额出售
如果余额<交易金额:
打印('您没有足够的资金购买',num_股份,'美元',每股价格,'每股')
打印('您当前的余额为',余额-交易金额',但',数量股份,'x',每股价格'=',数量股份*每股价格')
打印(“当前持有”,买入,“持有美元”,余额-交易金额,“现金”)
如果余额>交易金额:
打印(“当前持有”,买入,“持有美元”,余额-交易金额,“现金”)
main()

打印差额,
余额-交易金额
,但从未将此值设置为
余额
变量。基本上,您所做的事情与:

>>> 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\u amount
(或
balance-=transaction\u amount
),然后在打印功能中输入
balance

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")

这将使第一部分开始工作,接下来的部分将继续…

我建议您将此问题分解为一系列较小的问题。通过创建只处理较小问题的函数来实现这一点。在尝试在main中将每个函数连接在一起之前,先让它们工作起来

不管你是否买了东西,你都有余额。 你选择购买或出售。你在买什么或卖什么?如果你在销售,你拥有它吗?如果没有,可以吗?抛售(卖空)你不拥有的东西有什么不同吗

所以,从简单开始。假设你只买或卖香蕉。买100根香蕉和卖100根香蕉有区别吗?如果你处理的是买大于0的数字,卖小于0的数字,那就不太可能了

所以你有(现金)余额。当你“购买”时,你会返还成本。余额应按成本递减。如果你出售,余额应该增加成本

嗯。。你能卖一些你没有的东西吗?似乎你不仅需要跟踪你的余额,还需要跟踪你的“头寸”,即在所买卖商品中的持有量


如果你不能卖出比你自己更多的东西,或者买比你的余额更贵的香蕉,你需要有一种方法来处理这些问题。

在初始用户输入后,你在代码中的什么地方修改
余额
变量?@Hairo,我非常感谢你的回答,你介意使用给出的代码进行详细说明吗?我已经添加了,
balance=balance-transaction\u amount
,但余额只是将债务转回/@Christian一定要把它放在打印报表之前。@Haidro,如果我把它放在最后两张打印报表之后,它会正确地计算它,直到它碰到If报表,关于如果总价格>余额,它会说我没有足够的资金,并求助于之前购买的股票数量和余额……哦,伙计,很抱歉问了这么多问题。@你的朋友,只要你有打印功能,它有
余额-交易金额
,就把
余额=余额-交易金额