python帮助我完成这个函数

2024-09-30 10:42:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个函数,要求用户提供股票符号和名称配对,然后将其添加到名称字典。但到目前为止我只知道

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}

谢谢任何想帮忙的人


Tags: ofthe函数用户名称inputyouris
2条回答

这就是你要找的吗?很难从你的问题中知道:

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

这应该起作用:

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

相关问题 更多 >

    热门问题