[英]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.