Python - Interactive phonebook
<p>I need help with adding a name and phone number to a dictionary in ONE line with <code>raw_input</code>. It should look like this: <code>add John 123</code> (adds the name John with the number 123). Here is my code:</p>

<pre><code>def phonebook():
    pb={}
    while True:
        val,q,w=raw_input().split(" ")
        if val=='add':
            if q in pb:
                print
                print "This name already exists"
                print
            else:
                pb[q]=w #lÃ¤gger till namn + nummer i dictionary
        if val=='lookup':
            if q in pb:
                print
                print pb[q]
                print
            else:
                print "Name is not in phonebook"
                print 
</code></pre>

<p>Im getting the unpack error. Any tips? Is there another way to do it?</p>

<p>I think you get an unpack error when you lookup someone with "lookup john". The code looks for a third value in the split(" ") but doesn't find one.</p>

<p>I'm not sure if this might help:</p>

<pre><code>def phonebook():
pb={}
while True:
    vals = raw_input().split(" ")
    if vals[0] == 'add':
        q = vals[1]
        w = vals[2]
        if q in pb:
            print "This name already exists"
        else:
            pb[q]=w #lÃ¤gger till namn + nummer i dictionary
    elif vals[0]=='lookup':
        q = vals[1]
        if q in pb:
            print
            print str(q) + "'s number is: " + str(pb[q])
            print
        else:
            print "Name is not in phonebook"
            print
</code></pre>

<p>return me with:</p>

<pre><code>&gt;add j 12
&gt;add j 12
This name already exists
&gt;lookup j

j's number is: 12

&gt;lookup j 12

j's number is: 12

&gt;lookup k
Name is not in phonebook
</code></pre>

python split interactive