簡體   English   中英

來自用戶輸入的股票交易並在 Python 中顯示結果

[英]Stock transactions from user input and displaying the results in Python

嗨,我正在努力讓我的程序正常工作。 它需要接受多個股票交易並按順序顯示所有股票交易。 我該怎么做呢? 到目前為止,我是這樣寫的:

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

在 StockInput() 中的 while 循環開始時,您將 lst 重新初始化為 []。 這將清除您輸入的最后一個庫存。 你有:

while True:
    lst = []
    <other stuff>

你要:

lst = []
while True:
    <other stuff>

我建議通過讓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():使用簡單的for item in calcs():打印每個股票for item in calcs():

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM