In addition I think defaultdict doesn&#39;t avoid neither entering negatives nor something that isn&#39;t an integer, you could try something like this:<br><br>&gt;&gt;&gt; dictionary = {}<br>&gt;&gt;&gt; for _ in range(4):<br>
    key = input(&quot;Enter key: &quot;)<br>    value = 0<br>    while value &lt;= 0:<br>        try:<br>            value = int(input(&quot;Enter a value: &quot;))<br>        except ValueError:<br>            print(&quot;Value must be an integer!&quot;)<br>
        else:<br>            if(value &lt;= 0):<br>                print(&quot;Value must be greater than 0&quot;)<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>
&gt;&gt;&gt; dictionary<br>{&#39;a&#39;: 1, &#39;c&#39;: 3, &#39;b&#39;: 2, &#39;d&#39;: 4}<br>&gt;&gt;&gt; <br><br><div class="gmail_quote">2011/3/25 Hugo Arts <span dir="ltr">&lt;<a href="mailto:hugo.yoshi@gmail.com">hugo.yoshi@gmail.com</a>&gt;</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">&lt;<a href="mailto:robert.sjoblom@gmail.com">robert.sjoblom@gmail.com</a>&gt; wrote:<br>
</div><div class="im">&gt; Hi again, list! A quick question about dictionary behaviour. I have a<br>
&gt; dictionary of keys, and the user should be able to enter values into<br>
&gt; said dictionary. My idea was this:<br>
&gt;<br>
&gt; def addData(key, value):<br>
&gt;    dictionary[key] += int(value)<br>
&gt;    return None<br>
&gt;<br>
&gt; dictionary = {&quot;a&quot;:0, &quot;b&quot;:0, &quot;c&quot;:0, &quot;d&quot;:0}<br>
&gt;<br>
&gt; for key in dictionary:<br>
&gt;    a = input(&quot;Enter key: &quot;)<br>
&gt;    b = int(input(&quot;Enter value: &quot;))<br>
&gt;    try:<br>
&gt;        addData(a, b)<br>
&gt;    except:<br>
&gt;        continue<br>
&gt;<br>
<br>
</div>Why are you asking for the user to enter a key? you&#39;re iterating over<br>
all keys in the dictionary, so there&#39;s no need to ask the user for<br>
keys, you already know them. Why not do this:<br>
<br>
b = int(input(&quot;Enter value for key %s: &quot; % 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&#39;ll still need to check the user doesn&#39;t just<br>
enter 0 though.<br>
<br>
Another few points:<br>
* you don&#39;t ave to add return None in that function. If you get to the<br>
end of a function and there&#39;s no return statement, None is<br>
automatically returned.<br>
* don&#39;t use a blank except. *ever*. If you really want to catch<br>
everything, do &quot;except Exception:&quot; otherwise, catch what you expect to<br>
be thrown in this case, &quot;except KeyError&quot;<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>