Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80894 > unrolled thread

import math error

Started byryguy7272 <ryanshuell@gmail.com>
First post2014-11-16 12:07 -0800
Last post2014-11-16 17:55 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  import math error ryguy7272 <ryanshuell@gmail.com> - 2014-11-16 12:07 -0800
    Re: import math error Mayank Tripathi <oquanox@gmail.com> - 2014-11-16 20:48 +0000
    Re: import math error Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-16 14:41 -0700
    Re:import math error Dave Angel <davea@davea.name> - 2014-11-16 17:55 -0500

#80894 — import math error

Fromryguy7272 <ryanshuell@gmail.com>
Date2014-11-16 12:07 -0800
Subjectimport math error
Message-ID<a25f78c5-aa53-406f-a828-288ee4f7dcc5@googlegroups.com>
When I type 'import math', it seems like my Python recognizes this library.  Great.  When I try to run the following script, I get an error, which suggests (to me) the math library is not working correctly.

Script:
import math
def main():
	print "This program finds the real solutions to a quadratic"
	print
	a, b, c = input("Please enter the coefficients (a, b, c): ")
	discRoot = math.sqrt(b * b - 4 * a * c)
	root1 = (-b + discRoot) / (2 * a)
	root2 = (-b - discRoot) / (2 * a)
	print
	print "The solutions are:", root1, root2
main()

Error:
Traceback (most recent call last):
  File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in <module>
    main()
  File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
    a, b, c = input("Please enter the coefficients (a, b, c): ")
  File "<string>", line 1
    (1,1,2):
           ^
SyntaxError: unexpected EOF while parsing

The last line is line 11.  It seems like I need this, but that's what's causing the error.  Does anyone here have any idea what is wrong here?

Thanks.

[toc] | [next] | [standalone]


#80903

FromMayank Tripathi <oquanox@gmail.com>
Date2014-11-16 20:48 +0000
Message-ID<mailman.15900.1416170926.18130.python-list@python.org>
In reply to#80894

[Multipart message — attachments visible in raw view] — view raw

Why the colon in '(1,1,2)'?

On Mon Nov 17 2014 at 1:41:10 AM ryguy7272 <ryanshuell@gmail.com> wrote:

> When I type 'import math', it seems like my Python recognizes this
> library.  Great.  When I try to run the following script, I get an error,
> which suggests (to me) the math library is not working correctly.
>
> Script:
> import math
> def main():
>         print "This program finds the real solutions to a quadratic"
>         print
>         a, b, c = input("Please enter the coefficients (a, b, c): ")
>         discRoot = math.sqrt(b * b - 4 * a * c)
>         root1 = (-b + discRoot) / (2 * a)
>         root2 = (-b - discRoot) / (2 * a)
>         print
>         print "The solutions are:", root1, root2
> main()
>
> Error:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in <module>
>     main()
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
>     a, b, c = input("Please enter the coefficients (a, b, c): ")
>   File "<string>", line 1
>     (1,1,2):
>            ^
> SyntaxError: unexpected EOF while parsing
>
> The last line is line 11.  It seems like I need this, but that's what's
> causing the error.  Does anyone here have any idea what is wrong here?
>
> Thanks.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#80907

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-11-16 14:41 -0700
Message-ID<mailman.15904.1416174127.18130.python-list@python.org>
In reply to#80894
On Sun, Nov 16, 2014 at 1:07 PM, ryguy7272 <ryanshuell@gmail.com> wrote:
> When I type 'import math', it seems like my Python recognizes this library.  Great.  When I try to run the following script, I get an error, which suggests (to me) the math library is not working correctly.
>
> Script:
> import math
> def main():
>         print "This program finds the real solutions to a quadratic"
>         print
>         a, b, c = input("Please enter the coefficients (a, b, c): ")
>         discRoot = math.sqrt(b * b - 4 * a * c)
>         root1 = (-b + discRoot) / (2 * a)
>         root2 = (-b - discRoot) / (2 * a)
>         print
>         print "The solutions are:", root1, root2
> main()
>
> Error:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in <module>
>     main()
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
>     a, b, c = input("Please enter the coefficients (a, b, c): ")
>   File "<string>", line 1
>     (1,1,2):
>            ^
> SyntaxError: unexpected EOF while parsing
>
> The last line is line 11.  It seems like I need this, but that's what's causing the error.  Does anyone here have any idea what is wrong here?

This has nothing to do with the math module. You're using input() in
Python 2, which is equivalent to eval(raw_input()). Because of the
eval, the string that is entered needs to be a valid Python
expression. You apparently entered "(1,1,2):" which is not a valid
Python expression because of the colon.

By the way, general use of the Python 2 input and eval functions are
strongly discouraged because they can lead to execution of arbitrary
code when given untrusted input. The recommended approach is to use
raw_input() instead (which is renamed to just input() in Python 3) and
then parse the input properly without resorting to eval. For example,
the above could be written as something like:

    coeff_str = raw_input("Please enter the coefficients a, b, c:")
    a, b, c = map(float, coeff_str.split(','))

[toc] | [prev] | [next] | [standalone]


#80927

FromDave Angel <davea@davea.name>
Date2014-11-16 17:55 -0500
Message-ID<mailman.15919.1416178533.18130.python-list@python.org>
In reply to#80894
You forgot to specify your Python version here.

ryguy7272 <ryanshuell@gmail.com> Wrote in message:
> When I type 'import math', it seems like my Python recognizes this library.  Great.  When I try to run the following script, I get an error, which suggests (to me) the math library is not working correctly.

Nothing to do with the math library.  It's a syntax error. And it
 occurs before the first call into the math library.
 

> 
> Script:
> import math
> def main():
> 	print "This program finds the real solutions to a quadratic"
> 	print
> 	a, b, c = input("Please enter the coefficients (a, b, c): ")

Ouch. input is unsafe, anot recommended. That's why python 3
 renamed the raw_input function to input,  eliminating the unsafe
 version.  Nevertheless,  I'll assume you understand the risks,
 and we'll go on with your problem. 

> 	discRoot = math.sqrt(b * b - 4 * a * c)
> 	root1 = (-b + discRoot) / (2 * a)
> 	root2 = (-b - discRoot) / (2 * a)
> 	print
> 	print "The solutions are:", root1, root2
> main()
> 
> Error:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in <module>
>     main()
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
>     a, b, c = input("Please enter the coefficients (a, b, c): ")
>   File "<string>", line 1
>     (1,1,2):
>            ^
> SyntaxError: unexpected EOF while parsing

The error is occurring while parsing the user's input,  which
 should not have a colon.  The smallest flaw with using input ()
 is that user error cause strange looking exceptions.

> 
> The last line is line 11.  It seems like I need this, but that's what's causing the error.  Does anyone here have any idea what is wrong here?
> 
> Thanks.
> 


-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web