I need a function that Asks the user for a Stock Symbol and Name pairing then adds it to the Names dictionary. but all I have so far is
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}
thank you anyone who trys to help
2 Answers 2
This should work:
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
Output
what is the symbol of your stock: abc
what is the name of your stock: xyz
{'abc': 'xyz'}
answered Dec 12, 2014 at 3:28
Archit Verma
1,9095 gold badges29 silver badges47 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Archit Verma
@clutchcocobean Please let me know if you face any problem.
Is this what you are after? Difficult to know from your question:
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)
Prints:
what is the symbol of your stock: some symbol
what is the name of your stock: some name
{'some symbol': 'some name'}
answered Dec 12, 2014 at 3:28
Marcin
241k16 gold badges315 silver badges368 bronze badges
Comments
lang-py