I am a newbie too, but....<div>from collections import defaultdict</div><div><br></div><div>Then you can do:</div><div>data = defaultdict(int)</div><div>Everything (all keys) start at 0, so you can do "+=" with no problem.</div>
<div><br></div><div>I'm sure somebody else has a better solution though!</div><div><br></div><div>Cheers</div><div>Colin<br><br><div class="gmail_quote">On 25 March 2011 18:52, Robert Sjoblom <span dir="ltr"><<a href="mailto:robert.sjoblom@gmail.com">robert.sjoblom@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi again, list! A quick question about dictionary behaviour. I have a<br>
dictionary of keys, and the user should be able to enter values into<br>
said dictionary. My idea was this:<br>
<br>
def addData(key, value):<br>
dictionary[key] += int(value)<br>
return None<br>
<br>
dictionary = {"a":0, "b":0, "c":0, "d":0}<br>
<br>
for key in dictionary:<br>
a = input("Enter key: ")<br>
b = int(input("Enter value: "))<br>
try:<br>
addData(a, b)<br>
except:<br>
continue<br>
<br>
Which works fine, but the problem is that I need to ensure that every<br>
key gets a value above 0; the try/except is clearly not doing that<br>
(and it can't deal with b=int(input("Enter value:" )) because there's<br>
no try for that, which I suppose there should be). How would I go<br>
about making sure that each key gets a value above 0 in this case?<br>
Also, is there a better way to do what I'm trying to do than what I've<br>
come up with? I'm trying to translate a paper form into data arrays<br>
for later use (and this particular part is simple in comparison to<br>
some other parts where you get one name and multiple values to that<br>
name, which I have no idea how to solve, but I figure one step at a<br>
time, and the current step is this one), but I'm quite the newbie at<br>
programming. Still.<br>
<br>
best regards,<br>
Robert S.<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><img src="http://projecteuler.net/profile/colspeed.png"><br><br>
</div>