来自用户输入的股票交易,并在Python中显示结果
嗨,我正在努力让我的程序工作。它需要接受多个股票交易,并按顺序显示所有股票交易。我该怎么做呢?到目前为止,我是这样写的:来自用户输入的股票交易,并在Python中显示结果,python,python-3.x,Python,Python 3.x,嗨,我正在努力让我的程序工作。它需要接受多个股票交易,并按顺序显示所有股票交易。我该怎么做呢?到目前为止,我是这样写的: def main(): def stockInput(): while True: lst = [] stockName = input("Enter stock name: ") lst.append(stockName) stockNumber =
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>
为True时:
lst=[]
你想要:
lst = []
while True:
<other stuff>
lst=[]
尽管如此:
我建议通过让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()
中再次遍历每个项目,用一个简单的为calcs()中的项目打印每个库存:
哪个部分不起作用?当你尝试运行它时会发生什么?它只需要一个股票并输出它,但我需要它获取多个股票。我们不能在这里使用列表,而只使用主循环来获得相同的结果吗?是的,我们可以,这只是给它增加了一点组织性,如果需要对不同的股票条目进行相互比较的话,它会更容易扩展。当然,完整的解决方案/更常见的解决方案是创建一个名为stock
的类,然后从那里开始工作,但这是一个很好的中间过程。您的意思是创建一个基于用户输入信息创建股票的stock类吗?是否可以避免使用类而只使用函数来实现相同的结果?上面的代码就是通过将所有股票值保存在一个列表中来实现的。另一种方法是将列表标记到字典中,并将键设置为类似于stockx
的内容,在这里您可以更改x以获取某些股票。