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>The following line assume that you type exactly 3 words each separated by a space character:</p>

<pre><code>val, q, w = raw_input().split(" ")
</code></pre>

<p>If you have less or more than 3 words (which is the case when you use the lookup command, isn't it?), you will get an unpack error.</p>

<p>You could get the input in a unique variable and then test its first element to avoid error:</p>

<pre><code>in_ = raw_input().split(" ")
if in_[0] == 'add':
    # process add action
if in_[0] == 'lookup':
    # process lookup action
</code></pre>

<hr>

<p>bonus tip: you do not need to give the space character to <code>split</code> method since it is the default value:</p>

<pre><code>raw_input().split()  # will work as well
</code></pre>

python split interactive