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


Groups > comp.lang.python > #80907

Re: import math error

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; '"this': 0.03; '16,': 0.03; 'causing': 0.04; 'error:': 0.07; 'great.': 0.07; 'string': 0.09; 'here?': 0.09; 'main()': 0.09; 'parsing': 0.09; 'python': 0.11; 'def': 0.12; 'c):': 0.16; 'colon.': 0.16; 'eof': 0.16; 'expression.': 0.16; 'finds': 0.16; 'main():': 0.16; 'renamed': 0.16; 'script,': 0.16; 'subject:import': 0.16; 'syntaxerror:': 0.16; 'unexpected': 0.16; 'wrote:': 0.18; 'library': 0.18; 'properly': 0.19; 'entered': 0.20; 'written': 0.21; 'seems': 0.21; 'input': 0.22; 'import': 0.22; 'print': 0.22; 'parse': 0.24; 'math': 0.24; 'equivalent': 0.26; 'skip:" 40': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'strongly': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; '"",': 0.31; 'apparently': 0.31; 'correctly.': 0.31; 'anyone': 0.31; 'file': 0.32; 'run': 0.32; '(most': 0.33; '"the': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'library.': 0.36; 'module.': 0.36; 'error.': 0.37; 'example,': 0.37; 'wrong': 0.37; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; "you're": 0.61; 'real': 0.63; 'here': 0.66; '11.': 0.74
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=PtcYnD4HahRvt8nLXTSTYnTpvmjVMjBk3gr14Bia7Mo=; b=WaWwe+6dpln7NbOx/72GY1AIgVhHcTckZM+Rvu5Xa7K+37lR+qxcES2bsCkgJ7l1+g bCiJ0JMMbfrpL4WULejjHPJopDC5A5TyQv15gsnFgV7C9elNPqRKfvqAiNUt0RlIYU2e dBH2MoymocN72lo781Bspzn4GiuRnCG45/BHtFC8q9xSFWObHzRGp9e1WjSNh/tKtPPD 7tYkl4vSmgyKmv+ZaSdvTnMCKMW+xuGZcxfVrkhzA04H1BU6RlGCtL2JS3HYs0wq3GGB o1CpImhkddKT7CHGAj8vnm10FdFV9EVt1G+YJkaOijIYoUWJcz7bOXxe4xexMwOsLtoG j1sw==
X-Received by 10.70.61.136 with SMTP id p8mr25125182pdr.98.1416174124278; Sun, 16 Nov 2014 13:42:04 -0800 (PST)
MIME-Version 1.0
In-Reply-To <a25f78c5-aa53-406f-a828-288ee4f7dcc5@googlegroups.com>
References <a25f78c5-aa53-406f-a828-288ee4f7dcc5@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 16 Nov 2014 14:41:23 -0700
Subject Re: import math error
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15904.1416174127.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416174127 news.xs4all.nl 2848 [2001:888:2000:d::a6]:40300
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80907

Show key headers only | View raw


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(','))

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web