这是我所拥有的:
from pprint import pprint
Names = {}
Prices = {}
Exposure = {}
def AddName():
company_name = input("Please enter company name: ")
return company_name
def AddSymbol(company_name):
stock_symbol = input("Please enter a stock symbol: ")
Names[stock_symbol] = company_name
return Names
^^ 这会将 Names 字典更新为 {symbol:company name}
def AddPrices(stock_symbol):
buy = float(input("Please enter buying price of stock: "))
sell = float(input("Please enter current price of stock: "))
Prices[stock_symbol] = buy, sell
return Prices
^^ 这会生成一个 TypeError: unhashable type: 'dict' - 我想要的是更新价格字典,如 {symbol: buy price, sell price, symbol2: buy price, sell price etc..}
def printDicts(Names, Prices):
'''
For debug purposes, prints out contents of dictionaries
'''
print( "Names is now:" )
pprint(Names)
print("Prices now:")
pprint(Prices)
def main():
company_name = AddName()
stock_symbol = AddSymbol(company_name)
AddPrices(stock_symbol)
printDicts(Names, Prices)
main()
作为编程新手,我不完全确定如何解决这个问题。谢谢你的帮助!