[Tutor] if value not in dictionary, do a?

James Reynolds eire1130 at gmail.com
Fri Mar 25 16:46:13 CET 2011


On Fri, Mar 25, 2011 at 7:52 AM, Robert Sjoblom <robert.sjoblom at gmail.com>wrote:

> Hi again, list! A quick question about dictionary behaviour. I have a
> dictionary of keys, and the user should be able to enter values into
> said dictionary. My idea was this:
>
> def addData(key, value):
>    dictionary[key] += int(value)
>    return None
>
> dictionary = {"a":0, "b":0, "c":0, "d":0}
>
> for key in dictionary:
>    a = input("Enter key: ")
>    b = int(input("Enter value: "))
>    try:
>        addData(a, b)
>    except:
>        continue
>
> Which works fine, but the problem is that I need to ensure that every
> key gets a value above 0; the try/except is clearly not doing that
> (and it can't deal with b=int(input("Enter value:" )) because there's
> no try for that, which I suppose there should be). How would I go
> about making sure that each key gets a value above 0 in this case?
> Also, is there a better way to do what I'm trying to do than what I've
> come up with? I'm trying to translate a paper form into data arrays
> for later use (and this particular part is simple in comparison to
> some other parts where you get one name and multiple values to that
> name, which I have no idea how to solve, but I figure one step at a
> time, and the current step is this one), but I'm quite the newbie at
> programming. Still.
>
> best regards,
> Robert S.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>















How would I go
> about making sure that each key gets a value above 0 in this case?


I would probably add a separate function with an if / then type structure.
The separate function would simply be activated if the value is 0, at which
point the input would say "You didn't enter a value greater than 0, please
enter a value" or some such.

Also, is there a better way to do what I'm trying to do than what I've
> come up with?


I'm not 100% what you are trying to accomplish.

 I'm trying to translate a paper form into data arrays
> for later use (and this particular part is simple in comparison to
> some other parts where you get one name and multiple values to that
> name


This sounds like a database. For example, I have many attributes, some of
these I share with others, some are unique to me, some are variations on a
common theme.

So, for example, Jamie is 6'00, brown eyes, brown hair, etc. Or {Jamie :
[72, Brown, Brown,...]}

You can store the attributes in a list so long as they are always in the
same order, and then just look up the attributes as you need them, so
person[Jamie][0] will yield 72.

The other way to go, depending on your data of course and its complexity
would be to use a relational database. Python comes with SQlite in it, and
if you don't know SQL, this could be a good opportunity to learn some of
it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110325/7ef64bab/attachment.html>


More information about the Tutor mailing list