<div class="gmail_quote">On Mon, Feb 15, 2010 at 6:09 AM, Peter Anderson <span dir="ltr">&lt;<a href="mailto:peter.anderson@internode.on.net">peter.anderson@internode.on.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hi!<br>
<br>
I am trying to teach myself how to program in Python using Zelle&#39;s &quot;Python Programming: An Introduction to Computer Science&quot; (a very good text). At the same time I have decided to start with Python 3 (3.1.1). That means that I have to convert Zelle&#39;s example code to Python 3 (which generally I cope with).<br>


<br>
I&#39;m hoping that somebody can help with what&#39;s probably a very simple problem. There is a quadratic equation example involving multiple user inputs from the one &quot;input&quot; statement. The code works fine with Python 2.5 but when I convert it to Python 3 I get error messages. The code looks like:<br>


<br>
05 import math<br>
06<br>
07 def main():<br>
08 print(&quot;This program finds the real solutions to a quadratic\n&quot;)<br>
09<br>
10 a, b, c = input(&quot;Please enter the coefficients (a, b, c): &quot;)<br>
11<br>
12 &#39;&#39;&#39;<br>
13 a = int(input(&quot;Please enter the first coefficient: &quot;))<br>
14 b = int(input(&quot;Please enter the second coefficient: &quot;))<br>
15 c = int(input(&quot;Please enter the third coefficient: &quot;))<br>
16 &#39;&#39;&#39;<br>
17<br>
18 discrim = b * b - 4 * a * c<br>
19 ...<br>
<br>
25 main()<br>
<br>
Lines 08 to 12 are my Python 3 working solution but line 06 does not work in Python 3. When it runs it produces:<br>
<br>
Please enter the coefficients (a, b, c): 1,2,3<br>
Traceback (most recent call last):<br>
File &quot;C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py&quot;, line 25, in &lt;module&gt;<br>
File &quot;C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py&quot;, line 10, in main<br>
builtins.ValueError: too many values to unpack<br>
&gt;&gt;&gt;<br>
<br>
Clearly the problem lies in the input statement. If I comment out line 10 and remove the comments at lines 12 and 16 then the program runs perfectly. However, I feel this is a clumsy solution.<br>
<br>
Could somebody please guide me on the correct use of &quot;input&quot; for multiple values.<br></blockquote><div><br></div><div>First off, it&#39;s much healthier to use &quot;raw_input&quot;, and then try to convert it (float, int, whatever).</div>

<div><br></div><div>Example:</div><div><div>In [1]: x = input(&quot;Test: &quot;)</div><div>Test: print &quot;Ha ha, this is some input&quot;</div><div>------------------------------------------------------------</div><div>

   File &quot;&lt;string&gt;&quot;, line 1</div><div>     print &quot;Ha ha, this is some input&quot;</div><div>         ^</div><div>SyntaxError: invalid syntax</div><div><br></div><div><div>In [3]: x = raw_input(&quot;Test: &quot;)</div>

<div>Test: print &quot;ha ha, this is some raw_input&quot;</div><div><br></div><div>In [4]: x</div><div>Out[4]: &#39;print &quot;ha ha, this is some raw_input&quot;&#39;</div><div><br></div><div>The first one is susceptible to people inserting malicious code (at least in pre-3.0 versions).</div>

<div><br></div><div>The reason your code is throwing an error is input (or raw_input) gives you one value, and you&#39;re trying to assign it to three variables.</div><div><br></div><div>If you want to get 3 floating point values, the most concise (but maybe not so readable if you&#39;re not familiar with the syntax) is probably this:</div>

<div><br></div><div>a, b, c = [float(x) for x in raw_input(&quot;Please enter (a, b, c): &quot;).split()]</div><div><br></div><div>Example:</div><div><br></div><div><div>In [6]: a, b, c = [float(x) for x in raw_input(&quot;Please enter (a, b, c): &quot;).split()]</div>

<div>Please enter (a, b, c): 3.1 2.99 1</div><div><br></div><div>In [7]: a</div><div>Out[7]: 3.1000000000000001</div><div><br></div><div>In [8]: b</div><div>Out[8]: 2.9900000000000002</div><div><br></div><div>In [9]: c</div>

<div>Out[9]: 1.0</div><div><br></div><div>to understand what the above code is doing:</div><div><br></div><div>print raw_input(&quot;Please enter (a,b, c): &quot;)</div><div>print raw_input(&quot;Please enter (a,b, c): &quot;).split()</div>

<div>print [x for x in raw_input(&quot;Please enter (a,b, c): &quot;).split()]</div><div>print [float(x) for x in raw_input(&quot;Please enter (a,b, c): &quot;).split()]</div><div><br></div><div>HTH,</div><div>Wayne</div>

</div></div></div></div>