繁体   English   中英

python帮助我使用此功能

[英]python help me with this function

我需要一个函数,要求用户输入“股票代号”和“名称”配对,然后将其添加到“名称”字典中。 但到目前为止我只剩下

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}

谢谢任何尝试帮助的人

这应该工作:

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'}

这是你所追求的吗? 从您的问题很难知道:

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'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM