[Tutor] WindowsSE/Python User Need Help With Dictionaries

Ignacio Vazquez-Abrams ignacio@openservices.net
Thu, 30 Aug 2001 10:51:26 -0400 (EDT)


On Thu, 30 Aug 2001, 2Canadians wrote:

> I am working on a type of menu program using Python.  I have created a menu and want a user to be able to input names which would go into a dictionary.  I have created a dictionary and can't get the input to add another unit to the dictionary.  It will only take the last entered value.  If you have a hard time understanding this, here is a sample(not verbatum):
>
> import blah
>
> def menu1():
>     getConstantValue1=raw_input("BLAHBLAH")
>     getConstantValue2=raw_input("BLAHBLAH")
>     GetAmount=input("BLAHBLAH")
>     while GetAmount>0:
>         getNameList=raw_input("BLAH")
>         dictionary={"A":getConstantValue1 "B":getConstantValue2,                        "List":getNameList}  #THIS IS PART OF THE DICTIONARY NOT A NEW LINE!!
>         GetAmount=GetAmount - 1
>     print dictionary{}
> print menu1()
>
> Thanks,
> Specs

The problem is that you're overwriting the dicitonary through each loop:

---
def menu1():
  v1=raw_input('...')
  v2=raw_input('...')
  c=raw_input('...')
  l=[]
  while c>0:
    n=raw_input('...')
    d={'a':v1, 'b':v2, 'c':n}
    c=c-1
    l.append(d)
  return d

print menu1()
---

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>