[英]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.