[Tutor] Problem with "input" in Python 3

Wayne Werner waynejwerner at gmail.com
Mon Feb 15 14:10:57 CET 2010


On Mon, Feb 15, 2010 at 6:09 AM, Peter Anderson <
peter.anderson at internode.on.net> wrote:

> Hi!
>
> I am trying to teach myself how to program in Python using Zelle's "Python
> Programming: An Introduction to Computer Science" (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's example code to Python 3 (which generally I cope
> with).
>
> I'm hoping that somebody can help with what's probably a very simple
> problem. There is a quadratic equation example involving multiple user
> inputs from the one "input" 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:
>
> 05 import math
> 06
> 07 def main():
> 08 print("This program finds the real solutions to a quadratic\n")
> 09
> 10 a, b, c = input("Please enter the coefficients (a, b, c): ")
> 11
> 12 '''
> 13 a = int(input("Please enter the first coefficient: "))
> 14 b = int(input("Please enter the second coefficient: "))
> 15 c = int(input("Please enter the third coefficient: "))
> 16 '''
> 17
> 18 discrim = b * b - 4 * a * c
> 19 ...
>
> 25 main()
>
> Lines 08 to 12 are my Python 3 working solution but line 06 does not work
> in Python 3. When it runs it produces:
>
> Please enter the coefficients (a, b, c): 1,2,3
> Traceback (most recent call last):
> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py",
> line 25, in <module>
> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py",
> line 10, in main
> builtins.ValueError: too many values to unpack
> >>>
>
> 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.
>
> Could somebody please guide me on the correct use of "input" for multiple
> values.
>

First off, it's much healthier to use "raw_input", and then try to convert
it (float, int, whatever).

Example:
In [1]: x = input("Test: ")
Test: print "Ha ha, this is some input"
------------------------------------------------------------
   File "<string>", line 1
     print "Ha ha, this is some input"
         ^
SyntaxError: invalid syntax

In [3]: x = raw_input("Test: ")
Test: print "ha ha, this is some raw_input"

In [4]: x
Out[4]: 'print "ha ha, this is some raw_input"'

The first one is susceptible to people inserting malicious code (at least in
pre-3.0 versions).

The reason your code is throwing an error is input (or raw_input) gives you
one value, and you're trying to assign it to three variables.

If you want to get 3 floating point values, the most concise (but maybe not
so readable if you're not familiar with the syntax) is probably this:

a, b, c = [float(x) for x in raw_input("Please enter (a, b, c): ").split()]

Example:

In [6]: a, b, c = [float(x) for x in raw_input("Please enter (a, b, c):
").split()]
Please enter (a, b, c): 3.1 2.99 1

In [7]: a
Out[7]: 3.1000000000000001

In [8]: b
Out[8]: 2.9900000000000002

In [9]: c
Out[9]: 1.0

to understand what the above code is doing:

print raw_input("Please enter (a,b, c): ")
print raw_input("Please enter (a,b, c): ").split()
print [x for x in raw_input("Please enter (a,b, c): ").split()]
print [float(x) for x in raw_input("Please enter (a,b, c): ").split()]

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100215/3645c2bb/attachment-0001.htm>


More information about the Tutor mailing list