Why does my compiler say invalid syntax then highlight...?

Discussion in 'Python' started by Nathan Pinno, Mar 11, 2008.

  1. Nathan Pinno

    Nathan Pinno Guest

    Why does my compiler say invalid syntax and then highlight the
    quotation marks in the following code:

    # This program is to find primes.
    primes = []
    import math
    import gmpy
    while 1:
    run = int(raw_input("Do you want to calculate primes? 1 = yes and
    2 = no. "))
    if run == 1:
    y = int(raw_input("What number do you want to use as the final
    number to calculate with? "))
    x = int(raw_input("What number do you want to start
    calculating primes from? "))
    while x < 2:
    print "Number must be at least 2 for math reasons."
    else:
    while x < y:
    prime = math.cos(gmpy.pi(0) * gmpy.fac((x-1)) / x)
    if prime < 0:
    primes.append(x)
    else:
    print x " is not prime. " # highlights the final "
    here
    x = x + 1
    print primes
    elif run == 2:
    break
    else:
    print "Sorry, not a choice. Please enter your choice again."
    print "Goodbye."

    How do I fix such an invalid syntax?

    TIA,
    Nathan Pinno
    Nathan Pinno, Mar 11, 2008
    #1
    1. Advertising

  2. Nathan Pinno

    7stud Guest

    On Mar 10, 9:44 pm, Nathan Pinno <> wrote:
    > Why does my compiler say invalid syntax and then highlight the
    > quotation marks in the following code:
    >
    > # This program is to find primes.
    > primes = []
    > import math
    > import gmpy
    > while 1:
    >     run = int(raw_input("Do you want to calculate primes? 1 = yes and
    > 2 = no. "))
    >     if run == 1:
    >         y = int(raw_input("What number do you want to use as the final
    > number to calculate with? "))
    >         x = int(raw_input("What number do you want to start
    > calculating primes from? "))
    >         while x < 2:
    >             print "Number must be at least 2 for math reasons."
    >         else:
    >             while x < y:
    >                 prime = math.cos(gmpy.pi(0) * gmpy.fac((x-1)) / x)
    >                 if prime < 0:
    >                     primes.append(x)
    >                 else:
    >                     print x " is not prime. " # highlights the final "
    > here
    >                 x = x + 1
    >             print primes
    >     elif run == 2:
    >         break
    >     else:
    >         print "Sorry, not a choice. Please enter your choice again.."
    > print "Goodbye."
    >
    > How do I fix such an invalid syntax?
    >
    > TIA,
    > Nathan Pinno


    Try running this:

    input = raw_input("Hello
    world.")
    7stud, Mar 11, 2008
    #2
    1. Advertising

  3. Nathan Pinno

    7stud Guest

    On Mar 10, 9:44 pm, Nathan Pinno <> wrote:
    > Why does my compiler say invalid syntax and then highlight the
    > quotation marks in the following code:
    >
    > # This program is to find primes.
    > primes = []
    > import math
    > import gmpy
    > while 1:
    >     run = int(raw_input("Do you want to calculate primes? 1 = yes and
    > 2 = no. "))
    >     if run == 1:
    >         y = int(raw_input("What number do you want to use as the final
    > number to calculate with? "))
    >         x = int(raw_input("What number do you want to start
    > calculating primes from? "))
    >         while x < 2:
    >             print "Number must be at least 2 for math reasons."
    >         else:
    >             while x < y:
    >                 prime = math.cos(gmpy.pi(0) * gmpy.fac((x-1)) / x)
    >                 if prime < 0:
    >                     primes.append(x)
    >                 else:
    >                     print x " is not prime. " # highlights the final "
    > here
    >                 x = x + 1
    >             print primes
    >     elif run == 2:
    >         break
    >     else:
    >         print "Sorry, not a choice. Please enter your choice again.."
    > print "Goodbye."
    >
    > How do I fix such an invalid syntax?
    >
    > TIA,
    > Nathan Pinno


    Rather, try running this:

    print 10 "Hello world."
    7stud, Mar 11, 2008
    #3
  4. Nathan Pinno

    Guest

    I may have your problem:

    if prime < 0:
    primes.append(x)
    else:
    print x, "is not prime. " # highlights the final
    "

    you have to separate each output with a comma, so try adding one after
    x.
    , Mar 11, 2008
    #4
  5. Nathan Pinno

    Mensanator Guest

    On Mar 10, 10:44�pm, Nathan Pinno <> wrote:
    > Why does my compiler say invalid syntax and then highlight the
    > quotation marks in the following code:
    >
    > # This program is to find primes.


    Needs work.

    ## Do you want to calculate primes? 1 = yes and 2 = no. 1
    ## What number do you want to use as the final number to calculate
    with? 66
    ## What number do you want to start calculating primes from? 2
    ## 2 is not prime. False
    ## 3 is prime. True
    ## 4 is prime. False
    ## 5 is prime. True
    ## 6 is not prime. True
    ## 7 is prime. True
    ## 8 is not prime. True
    ## 9 is not prime. True
    ## 10 is not prime. True
    ## 11 is prime. True
    ## 12 is not prime. True
    ## 13 is prime. True
    ## 14 is not prime. True
    ## 15 is not prime. True
    ## 16 is not prime. True
    ## 17 is prime. True
    ## 18 is not prime. True
    ## 19 is prime. True
    ## 20 is not prime. True
    ## 21 is prime. False
    ## 22 is not prime. True
    ## 23 is prime. True
    ## 24 is prime. False
    ## 25 is prime. False
    ## 26 is not prime. True
    ## 27 is not prime. True
    ## 28 is not prime. True
    ## 29 is prime. True
    ## 30 is not prime. True
    ## 31 is not prime. False
    ## 32 is prime. False
    ## 33 is prime. False
    ## 34 is not prime. True
    ## 35 is prime. False
    ## 36 is not prime. True
    ## 37 is not prime. False
    ## 38 is not prime. True
    ## 39 is prime. False
    ## 40 is prime. False
    ## 41 is prime. True
    ## 42 is not prime. True
    ## 43 is not prime. False
    ## 44 is not prime. True
    ## 45 is prime. False
    ## 46 is not prime. True
    ## 47 is prime. True
    ## 48 is not prime. True
    ## 49 is not prime. True
    ## 50 is prime. False
    ## 51 is not prime. True
    ## 52 is not prime. True
    ## 53 is not prime. False
    ## 54 is not prime. True
    ## 55 is prime. False
    ## 56 is prime. False
    ## 57 is not prime. True
    ## 58 is not prime. True
    ## 59 is not prime. False
    ## 60 is prime. False
    ## 61 is prime. True
    ## 62 is not prime. True
    ## 63 is prime. False
    ## 64 is not prime. True
    ## 65 is prime. False
    ## Do you want to calculate primes? 1 = yes and 2 = no. 2
    ## Goodbye.
    Mensanator, Mar 11, 2008
    #5
  6. Nathan Pinno

    Chris Guest

    On Mar 11, 5:44 am, Nathan Pinno <> wrote:
    > Why does my compiler say invalid syntax and then highlight the
    > quotation marks in the following code:
    >
    > # This program is to find primes.
    > primes = []
    > import math
    > import gmpy
    > while 1:
    >     run = int(raw_input("Do you want to calculate primes? 1 = yes and
    > 2 = no. "))
    >     if run == 1:
    >         y = int(raw_input("What number do you want to use as the final
    > number to calculate with? "))
    >         x = int(raw_input("What number do you want to start
    > calculating primes from? "))
    >         while x < 2:
    >             print "Number must be at least 2 for math reasons."
    >         else:
    >             while x < y:
    >                 prime = math.cos(gmpy.pi(0) * gmpy.fac((x-1)) / x)
    >                 if prime < 0:
    >                     primes.append(x)
    >                 else:
    >                     print x " is not prime. " # highlights the final "
    > here
    >                 x = x + 1
    >             print primes
    >     elif run == 2:
    >         break
    >     else:
    >         print "Sorry, not a choice. Please enter your choice again.."
    > print "Goodbye."
    >
    > How do I fix such an invalid syntax?
    >
    > TIA,
    > Nathan Pinno


    The reason that line is giving you a syntax error is because you have
    no comma between your variable and the string. Same reason you can do
    something like 'print a b c' but instead have to use 'print a, b, c'
    Chris, Mar 11, 2008
    #6
  7. Nathan Pinno

    Duncan Booth Guest

    Mensanator <> wrote:

    > On Mar 10, 10:44‹¨«pm, Nathan Pinno <> wrote:
    >> Why does my compiler say invalid syntax and then highlight the
    >> quotation marks in the following code:
    >>
    >> # This program is to find primes.

    >
    > Needs work.


    Be fair. The OP hadn't managed to figure out why the program wasn't
    running, so you can't expect him to have got all the bugs out of the code
    yet. And he only asked about the specific syntax error not the entire
    solution to his homework.

    My advice to Nathan would be:

    1. If you get a weird syntax error that you don't understand try cutting
    the code down to just the bit which generates the error.

    2. Play around in the interactive interpreter to see what works and what
    doesn't.

    3. If you don't understand why the code doesn't work then get a stuffed
    toy, cardboard cutout of a person, or the least technical member of your
    family and explain to them in great detail exactly why the code must
    (despite error messages to the contrary) be correct. Usually you'll spot
    the problem half way through the explanation.

    4. If you post to this list then post the full error message and traceback.
    That way we don't have to guess which quotation marks are the problem.
    Duncan Booth, Mar 11, 2008
    #7
  8. Nathan Pinno

    Mensanator Guest

    On Mar 11, 3:36 am, Duncan Booth <> wrote:
    > Mensanator <> wrote:
    > > On Mar 10, 10:44‹¨«pm, Nathan Pinno <> wrote:
    > >> Why does my compiler say invalid syntax and then highlight the
    > >> quotation marks in the following code:

    >
    > >> # This program is to find primes.

    >
    > > Needs work.

    >
    > Be fair.


    Being helpful isn't fair?

    > The OP hadn't managed to figure out why the program wasn't
    > running, so you can't expect him to have got all the bugs out of the code
    > yet.


    The bug had already been answered.

    If you fail to implement your premise correctly, you have a bug.

    It doesn't matter if the code is bug-free if the premise is false.

    In this case, the premise that he can determine primes this way
    is false. He will have to abandon the cos() thing, for although it
    works in theory, he'll never get it to work in practice.

    Besides, the cos() is a red herring (hint: cos(pi*n)). The problem
    can be made to work EXACTLY (at least up to z=10000) by using
    rationals
    (which gmpy supports).

    His problem is going to take much more than fixing a syntax error.

    > And he only asked about the specific syntax error not the entire
    > solution to his homework.


    If this is indeed homework, and he's supposed to come up with a
    factorial algorithm, I seriously doubt the teacher would accept
    gmpy.fac(z-1), so I assume it isn't.

    >
    > My advice to Nathan would be:
    >
    > 1. If you get a weird syntax error that you don't understand try cutting
    > the code down to just the bit which generates the error.
    >
    > 2. Play around in the interactive interpreter to see what works and what
    > doesn't.
    >
    > 3. If you don't understand why the code doesn't work then get a stuffed
    > toy, cardboard cutout of a person, or the least technical member of your
    > family and explain to them in great detail exactly why the code must
    > (despite error messages to the contrary) be correct. Usually you'll spot
    > the problem half way through the explanation.
    >
    > 4. If you post to this list then post the full error message and traceback..
    > That way we don't have to guess which quotation marks are the problem.
    Mensanator, Mar 11, 2008
    #8
  9. Nathan Pinno

    Nathan Pinno Guest

    On Mar 11, 1:12 pm, Mensanator <> wrote:
    > On Mar 11, 3:36 am, Duncan Booth <> wrote:
    >
    > > Mensanator <> wrote:
    > > > On Mar 10, 10:44‹¨«pm, Nathan Pinno <> wrote:
    > > >> Why does my compiler say invalid syntax and then highlight the
    > > >> quotation marks in the following code:

    >
    > > >> # This program is to find primes.

    >
    > > > Needs work.

    >
    > > Be fair.

    >
    > Being helpful isn't fair?
    >
    > > The OP hadn't managed to figure out why the program wasn't
    > > running, so you can't expect him to have got all the bugs out of the code
    > > yet.

    >
    > The bug had already been answered.
    >
    > If you fail to implement your premise correctly, you have a bug.
    >
    > It doesn't matter if the code is bug-free if the premise is false.
    >
    > In this case, the premise that he can determine primes this way
    > is false. He will have to abandon the cos() thing, for although it
    > works in theory, he'll never get it to work in practice.
    >
    > Besides, the cos() is a red herring (hint: cos(pi*n)). The problem
    > can be made to work EXACTLY (at least up to z=10000) by using
    > rationals
    > (which gmpy supports).
    >
    > His problem is going to take much more than fixing a syntax error.
    >
    > > And he only asked about the specific syntax error not the entire
    > > solution to his homework.

    >
    > If this is indeed homework, and he's supposed to come up with a
    > factorial algorithm, I seriously doubt the teacher would accept
    > gmpy.fac(z-1), so I assume it isn't.
    >
    >
    >
    > > My advice to Nathan would be:

    >
    > > 1. If you get a weird syntax error that you don't understand try cutting
    > > the code down to just the bit which generates the error.

    >
    > > 2. Play around in the interactive interpreter to see what works and what
    > > doesn't.

    >
    > > 3. If you don't understand why the code doesn't work then get a stuffed
    > > toy, cardboard cutout of a person, or the least technical member of your
    > > family and explain to them in great detail exactly why the code must
    > > (despite error messages to the contrary) be correct. Usually you'll spot
    > > the problem half way through the explanation.

    >
    > > 4. If you post to this list then post the full error message and traceback.
    > > That way we don't have to guess which quotation marks are the problem.


    Yep, got it fixed, and no, it is not a homework problem...just a
    project for fun to keep up my Python coding in my memory intact
    (apparently, it doesn't work as well when I've been up early 3 days in
    a row... :p ).

    Now I just have to figure out why it isn't working the way it is
    supposed to. It was given to me by someone in the sci.math area as an
    alternative to working with 1 < x < n.
    Nathan Pinno, Mar 12, 2008
    #9
  10. Nathan Pinno

    Mensanator Guest

    On Mar 11, 9:50 pm, Nathan Pinno <> wrote:
    > On Mar 11, 1:12 pm, Mensanator <> wrote:
    >
    >
    >
    >
    >
    > > On Mar 11, 3:36 am, Duncan Booth <> wrote:

    >
    > > > Mensanator <> wrote:
    > > > > On Mar 10, 10:44‹¨«pm, Nathan Pinno <> wrote:
    > > > >> Why does my compiler say invalid syntax and then highlight the
    > > > >> quotation marks in the following code:

    >
    > > > >> # This program is to find primes.

    >
    > > > > Needs work.

    >
    > > > Be fair.

    >
    > > Being helpful isn't fair?

    >
    > > > The OP hadn't managed to figure out why the program wasn't
    > > > running, so you can't expect him to have got all the bugs out of the code
    > > > yet.

    >
    > > The bug had already been answered.

    >
    > > If you fail to implement your premise correctly, you have a bug.

    >
    > > It doesn't matter if the code is bug-free if the premise is false.

    >
    > > In this case, the premise that he can determine primes this way
    > > is false. He will have to abandon the cos() thing, for although it
    > > works in theory, he'll never get it to work in practice.

    >
    > > Besides, the cos() is a red herring (hint: cos(pi*n)). The problem
    > > can be made to work EXACTLY (at least up to z=10000) by using
    > > rationals
    > > (which gmpy supports).

    >
    > > His problem is going to take much more than fixing a syntax error.

    >
    > > > And he only asked about the specific syntax error not the entire
    > > > solution to his homework.

    >
    > > If this is indeed homework, and he's supposed to come up with a
    > > factorial algorithm, I seriously doubt the teacher would accept
    > > gmpy.fac(z-1), so I assume it isn't.

    >
    > > > My advice to Nathan would be:

    >
    > > > 1. If you get a weird syntax error that you don't understand try cutting
    > > > the code down to just the bit which generates the error.

    >
    > > > 2. Play around in the interactive interpreter to see what works and what
    > > > doesn't.

    >
    > > > 3. If you don't understand why the code doesn't work then get a stuffed
    > > > toy, cardboard cutout of a person, or the least technical member of your
    > > > family and explain to them in great detail exactly why the code must
    > > > (despite error messages to the contrary) be correct. Usually you'll spot
    > > > the problem half way through the explanation.

    >
    > > > 4. If you post to this list then post the full error message and traceback.
    > > > That way we don't have to guess which quotation marks are the problem.

    >
    > Yep, got it fixed, and no, it is not a homework problem...just a
    > project for fun to keep up my Python coding in my memory intact
    > (apparently, it doesn't work as well when I've been up early 3 days in
    > a row... :p ).
    >
    > Now I just have to figure out why it isn't working the way it is
    > supposed to.


    Did you see my hints, or are you keen to work it out?

    My solution tested up to z=10000. Factorial starts
    getting sluggish there and is utterly intractable
    for really large numbers, such as RSA.

    > It was given to me by someone in the sci.math area as an
    > alternative to working with 1 < x < n.- Hide quoted text -
    >
    > - Show quoted text -
    Mensanator, Mar 12, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,754
    Smokey Grindel
    Dec 2, 2006
  2. Mufasa
    Replies:
    6
    Views:
    876
    Scott M.
    Jul 27, 2007
  3. Cirene
    Replies:
    2
    Views:
    2,123
    Cirene
    Jun 6, 2008
  4. Replies:
    0
    Views:
    259
  5. Daniel Di Vita
    Replies:
    2
    Views:
    194
    Anthony Jones
    Apr 21, 2006
Loading...

Share This Page