Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="http://docs.python.org/library/functions.html#raw_input" rel="noreferrer"><code>raw_input()</code></a> instead of <code>input()</code>.</p> <pre><code># Python 2.5.4 &gt;&gt;&gt; a = raw_input() 3, 4, 5 &gt;&gt;&gt; a '3, 4, 5' &gt;&gt;&gt; b = a.split(', ') &gt;&gt;&gt; b ['3', '4', '5'] &gt;&gt;&gt; [s.strip() for s in raw_input().split(",")] # one liner 3, 4, 5 ['3', '4', '5'] </code></pre> <p>The misleadingly names <a href="http://docs.python.org/library/functions.html#input" rel="noreferrer"><code>input</code></a> function does not do what you'd expect it to. It actually evaluates the input from stdin as python code.</p> <p>In your case it turns out that what you then have is a tuple of numbers in <code>a</code>, all parsed and ready for work, but generally you don't really want to use this curious side effect. Other inputs can cause any number of things to happen.</p> <p>Incidentally, in Python 3 they fixed this, and now the <a href="http://docs.python.org/3.1/library/functions.html#input" rel="noreferrer"><code>input</code></a> function does what you'd expect.</p> <p>Two more things:</p> <ol> <li>You don't need to <code>import string</code> to do simple string manipulations. </li> <li>Like mjv <a href="http://#1397838" rel="noreferrer">said</a>, to split a tuple or a list into several variables, you can 'unpack' it. This will not be feasible if you don't know how long the list will be, though.</li> </ol> <p>Unpacking:</p> <pre><code>&gt;&gt;&gt; l = (1,2,3,4,5) &gt;&gt;&gt; a,b,c,d,e = l &gt;&gt;&gt; e 5 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload