我需要一个功能,要求用户输入Stock Symbol和Name配对,然后将其添加到Names字典中。但到目前为止我所拥有的只是
def addname(x,y):
dicname = {(x): (y)}
return dicname;
stocksymbol = (input("what is the symbol of your stock"))
stockname = (input("what is the name of your stock"))
addname(stocksymbol, stockname)
dnames = {dicname}
谢谢任何寻求帮助的人
答案 0 :(得分:1)
这应该有效:
def addname(x,y):
dicname = {(x): (y)}
return dicname
stocksymbol = (raw_input("what is the symbol of your stock: "))
stockname = (raw_input("what is the name of your stock: "))
dnames = addname(stocksymbol, stockname)
print dnames
<强>输出强>
what is the symbol of your stock: abc
what is the name of your stock: xyz
{'abc': 'xyz'}
答案 1 :(得分:0)
这就是你追求的吗?很难从你的问题中得知:
dictname = {}
def addname(x,y):
global dictname
dictname[x] = y
stocksymbol = input("what is the symbol of your stock: ")
stockname = input("what is the name of your stock: ")
addname(stocksymbol, stockname)
print(dictname)
打印:
what is the symbol of your stock: some symbol
what is the name of your stock: some name
{'some symbol': 'some name'}