[Tutor] User Made Dictionaries

Steven D'Aprano steve at pearwood.info
Tue Mar 29 00:51:28 CEST 2011


michael scott wrote:

> I was thinking something like this for my general flow (this is all just fake 
> code trying to represent my thought process)
> 
> mold =  { "name"              : " ",
>                  "age"                : " ",
>                  "charm_point" : " ",
>                  "profile_pic"     : " ",
>                  "pic_quantity"  : 0 ,
>                  "bio"                  : " "}
> 
> 
> chiaki = copy.copy(mold)
> #have the user fill in the data here
> natalie =  copy.copy(mold)
> #have the user fill in the data here
> # etc...
> 
> 
> But my question is how do I repeatedly automate new names for the  dictionaries? 
> Like how do I get the user to create the dictionary name  (chiaki, natalie, 
> etc...)? 


You don't.

Instead of something like this:

chiaki = {...}
natalie = {...}
...

You have a second dictionary, keyed by the names:

girls = {'chiaki': {...},
          'natalie': {...},
          ...
         }


Something like this:

girls = {}

while True:
     template = copy.copy(mold)
     name = raw_input("What's the name of an actress?")  # See below.
     if name == "exit":
          break  # Exit the loop.
     #have the user fill in rest of the data here
     girls[name] = template


Or you could use a real database, like sqlite, or even a *real* database.

If you are using Python 3 or better, change raw_input to input.



-- 
Steven


More information about the Tutor mailing list