来自用户输入的股票交易并在Python中显示结果

时间:2015-10-26 19:47:34

标签: python python-3.x

您好,我正在尝试让我的程序正常运行。它需要接受多个股票交易并按顺序显示所有股票交易。我该怎么做呢?到目前为止我写的是这样的:

def main():
    def stockInput():

         while True:
            lst = []
            stockName = input("Enter stock name: ")
            lst.append(stockName)
            stockNumber = int(input("Enter number of shares Joe bought: "))
            lst.append(stockNumber)
            stockPurchase = float(input("Enter the purchase price of the stock: "))
            lst.append(stockPurchase)
            stockSell = float(input("Enter the sell price of the stock: "))
            lst.append(stockSell)
            brokerComm = float(input("Enter the broker commission: "))
            lst.append(brokerComm)
            finishInput = input("If you want to quit, type quit, otherwise type enter to reenter information:")
            if finishInput == "quit":
                return lst

    def calcs():
        lst2 = []
        list1 = stockInput()
        print(list1)
        stockName = list1[0]
        print(stockName)
        stockNumber = list1[1]
        print(stockNumber)
        stockPurchase = list1[2]
        print(stockPurchase)
        stockSell = list1[3]
        print(stockSell)
        brokerComm = list1[4]
        print(brokerComm)

        amountPaid = stockNumber * stockPurchase

        paidCommission = amountPaid * brokerComm

        amountSold = stockNumber * stockSell

        soldCommission = amountSold * brokerComm

        profitLoss = (amountSold - soldCommission) - (amountPaid + paidCommission)
        print(amountPaid)
        print(paidCommission)
        print(amountSold)
        print(soldCommission)
        print("The total profit or loss is ", profitLoss)
        lst2.append(stockName)
        lst2.append(amountPaid)
        lst2.append(paidCommission)
        lst2.append(amountSold)
        lst2.append(soldCommission)
        lst2.append(profitLoss)
        return lst2

    def display():
        list2 = calcs()

        print("The name of the stock is : ", list2[0])
        print("The amount of money paid is : ", list2[1])
        print("The amount of money paid in commission is : ", list2[2])
        print("The amount of money sold the stock for is : ", list2[3])
        print("The amount of money paid in commission for the sold stock is : ", list2[4])
        print("The amount of money made or lost is: ", list2[5])


    display()

main()

2 个答案:

答案 0 :(得分:0)

在StockInput()内的while循环开始时,将lst重新初始化为[]。那将擦拭你输入的最后一只股票。你有:

while True:
    lst = []
    <other stuff>

你想:

lst = []
while True:
    <other stuff>

答案 1 :(得分:0)

我建议通过让stockInput()返回列表列表而不仅仅是列表来稍微改变一下以适应多个库存输入。你可以这样做:

def stockInput():
    lst = []
     while True:
        stock = []
        stockName = input("Enter stock name: ")
        stock.append(stockName)
        stockNumber = int(input("Enter number of shares Joe bought: "))
        stock.append(stockNumber)
        stockPurchase = float(input("Enter the purchase price of the stock: "))
        stock.append(stockPurchase)
        stockSell = float(input("Enter the sell price of the stock: "))
        stock.append(stockSell)
        brokerComm = float(input("Enter the broker commission: "))
        stock.append(brokerComm)
        lst.append(stock)
        finishInput = input("If you want to quit, type quit, otherwise type enter to reenter information:")
        if finishInput == "quit":
            return lst

这样,在calc中,您可以遍历列表列表:

def calcs():
    lst2 = []
    allstocks = stockInput()
    for stock in all stocks:
        print(stock)
        stockName = stock[0]
        print(stockName)
        stockNumber = stock[1]
        print(stockNumber)
        stockPurchase = stock[2]
        print(stockPurchase)
        stockSell = stock[3]
        print(stockSell)
        brokerComm = stock[4]
        print(brokerComm)

        #HERE IS WHERE YOU DO ALL YOUR CALCULATIONS
        calculatedStock = [HERE IS WHERE YOU SET ALL YOUR NECESSARY DATA]
        lst2.append(calculatedStock)
    return lst2

然后,您可以在display()中再次遍历每个项目,以使用简单的for item in calcs():

打印每个股票