我不确定是否需要发布所有内容,但我认为我会将其全部包括在内以防万一。我正在处理的作业使用3个词典和功能,允许用户添加股票,推荐销售,并退出当前我的GetSale或推荐销售功能的问题。 GetSale函数应该找到卖出股票的最大期望值。股票的预期销售价值是当前利润减去股票的未来价值:
Expected Sale value = ( ( Current Price - Buy Price ) - Risk * CurrentPrice ) * Shares
GetSale函数应为投资组合中的每只股票计算此值,并返回具有最高预期销售价值的股票代码。当我在Main函数下使用选项2调用GetSale函数时,我总是得到"最畅销股票的利润率为0。"即使进入不同的股票。我的GetSale函数是否已经编码,这就是它返回的内容,它甚至没有检查字典我不熟悉它,所以不确定我哪里出错了。
Names = {}
Prices = {}
Exposure = {}
def AddName(StockNames, StockPrices, StockExposure):
TheStockName = input("Input stock name. ")
Prompt = "Input the stock symbol for " + TheStockName
Symbol = input(Prompt)
StockNames[Symbol] = TheStockName
StockPrices[Symbol] = None
StockExposure[Symbol] = None
return StockNames
def AddPrices(StockPrices):
while True:
Symbol = input("Input stock symbol for the stock and add price next. ")
if Symbol in Prices:
Prompt = input("Current buying price of " + Symbol + ": ")
BuyPrice = float(Prompt)
Prompt = input("Current selling price of " + Symbol + ": ")
SellPrice = float(Prompt)
StockPrices[Symbol] = [BuyPrice, SellPrice]
return StockPrices
else:
print("This stock does not exist.")
def AddExposure(StockExposure):
while True:
Symbol = input("Input stock symbol for the stock and add exposure next. ")
if Symbol in Exposure:
Prompt = input("Current number of " + Symbol + " stocks that have been purchased? ")
SharesOwned = float(Prompt)
Prompt = input("Current risk factor of " + Symbol + " share ownership? ")
RiskFactor = float(Prompt)
StockExposure[Symbol] = [SharesOwned, RiskFactor]
return StockExposure
else:
print("This stock does not exist.")
def AddStock():
AddName(Names, Prices, Exposure)
AddPrices(Prices)
AddExposure(Exposure)
def GetSale(Names, Prices, Exposure):
HighestStock = 0
HighestStockName = ''
for Stock in Names:
TotalProfit = ((Prices[Stock][1] - Prices[Stock][0]) - Exposure[Stock][1] * Prices[Stock] [0]) * Exposure[Stock][0]
if (TotalProfit > HighestStock):
HighestStock = TotalProfit
HighestStockName = Stock
print("Highest selling stock is", HighestStockName, "with a ", HighestStock, "profit margin.")
def Main():
keepGoing = True
while keepGoing:
print("Menu")
print("1: Add a stock")
print("2: Find the highest sale price")
print("3: Exit")
userResponseString = input("Input selection (1, 2 or 3): ")
try:
userInput = int(userResponseString)
except:
print("Invalid input.")
userChoice = 0
if (userInput == 1):
AddStock()
elif (userInput == 2):
GetSale(Names, Prices, Exposure)
else:
(userInput == 3)
print("Ending stock program.")
keepGoing = False
Main()