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

Robert Sjoblom robert.sjoblom at gmail.com
Fri Mar 25 17:11:00 CET 2011

-- Hugo Arts wrote --

>Why are you asking for the user to enter a key? you're iterating over
>all keys in the dictionary, so there's no need to ask the user for
>keys, you already know them. Why not do this:

>b = int(input("Enter value for key %s: " % key))
>dictionary[key] = b

I have a great answer to that, actually. I simply forgot that you
could. I _knew_ that there was a better way of doing what I was trying
to accomplish, but I needed the user to know which key was being
entered, and I completely forgot about %s.

>* don't use a blank except. *ever*. If you really want to catch
>everything, do "except Exception:" otherwise, catch what you expect to
>be thrown in this case, "except KeyError"

I received your email just as I left my computer, and didn't want to
type out an answer on my phone (because smartphones aren't smart
enough for that yet), and my first reaction was "why?" followed by "I
need my program to continue even if I get the errors at this part, so
why?" I soon realized that catching all errors would mean never seeing
the errors you might not foresee. I know that I could get either
ValueError or KeyError, but there could be something else somewhere
else in the code that could send an error (unlikely as it is) down
that path and with a blank except I would never actually see that
happen. Am I somewhat close to the real reason why?

-- James Reynolds wrote --

>>  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.

Both of those are great ideas that I'm happy to steal -- thank you! I
dabbled some in databases a couple of years back, but I never really
focused on learning any of it properly, so what little I knew has long
since been lost to me. I think that for now it'll be enough to store
the data in a list but I'll definitely keep databases in mind. I know
that I'll probably have to learn them sooner or later, if not for this
project then for some other project down the road.

My issue (small as it may have been) have been resolved by the answers
I've received today, so thank you all for your time and help.
best regards,
Robert S.

More information about the Tutor mailing list