In addition I think defaultdict doesn't avoid neither entering negatives nor something that isn't an integer, you could try something like this:<br><br>>>> dictionary = {}<br>>>> for _ in range(4):<br>
key = input("Enter key: ")<br> value = 0<br> while value <= 0:<br> try:<br> value = int(input("Enter a value: "))<br> except ValueError:<br> print("Value must be an integer!")<br>
else:<br> if(value <= 0):<br> print("Value must be greater than 0")<br> dictionary[key] = value<br><br> <br>Enter key: a<br>Enter a value: 1<br>Enter key: b<br>Enter a value: 0<br>
Value must be greater than 0<br>Enter a value: -1<br>Value must be greater than 0<br>Enter a value: sdf<br>Value must be an integer!<br>Enter a value: 2<br>Enter key: c<br>Enter a value: 3<br>Enter key: d<br>Enter a value: 4<br>
>>> dictionary<br>{'a': 1, 'c': 3, 'b': 2, 'd': 4}<br>>>> <br><br><div class="gmail_quote">2011/3/25 Hugo Arts <span dir="ltr"><<a href="mailto:hugo.yoshi@gmail.com">hugo.yoshi@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">On Fri, Mar 25, 2011 at 12:52 PM, Robert Sjoblom<br>
<div class="im"><<a href="mailto:robert.sjoblom@gmail.com">robert.sjoblom@gmail.com</a>> wrote:<br>
</div><div class="im">> 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>
<br>
</div>Why are you asking for the user to enter a key? you're iterating over<br>
all keys in the dictionary, so there's no need to ask the user for<br>
keys, you already know them. Why not do this:<br>
<br>
b = int(input("Enter value for key %s: " % key))<br>
dictionary[key] = b<br>
<br>
This way you can be sure a value is entered for every key, and no key<br>
is entered twice. You'll still need to check the user doesn't just<br>
enter 0 though.<br>
<br>
Another few points:<br>
* you don't ave to add return None in that function. If you get to the<br>
end of a function and there's no return statement, None is<br>
automatically returned.<br>
* don't use a blank except. *ever*. If you really want to catch<br>
everything, do "except Exception:" otherwise, catch what you expect to<br>
be thrown in this case, "except KeyError"<br>
<font color="#888888"><br>
Hugo<br>
</font><div><div></div><div class="h5">_______________________________________________<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>
</div></div></blockquote></div><br>