简体   繁体   中英

Stock transactions from user input and displaying the results in Python

Hi I am trying to get my program to work. It needs to accept multiple stock transactions and display all of the stock transactions in a sequence. How should I go about doing this? I have so far written it like this:

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

At the beginning of your while loop inside StockInput() you reinitialize lst to []. That'll wipe clean the last stock you entered. You have:

while True:
    lst = []
    <other stuff>

You want:

lst = []
while True:
    <other stuff>

I would suggest changing things slightly to accomodate for more than one stock input by having stockInput() return a list of lists rather than just a list. you can do this by:

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

this way, inside of calc, you can then iterate through your list of lists:

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

And then you can iterate through each item again in display() to print each stock with a simple for item in calcs():

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM