Syntax error in a while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • calvin dong
    New Member
    • Oct 2011
    • 8

    Syntax error in a while loop

    In my program to brute force all the factors (not including 1) of a number, there is a syntax error in my while loop. The code looks like this.

    Code:
    number = raw_input(" Enter a number ")
    x = 2
    coollist = []
    while x <= sqrt(int(number):
        if number % loop == 0:
            coollist.append(x)
        else:
            x += 1
    print coollist
    The syntax error reads


    admin-trs-imac-2:programmingst uff yandong$ python factors.py
    File "factors.py ", line 4
    while x <= sqrt(int(number ):
    ^
    SyntaxError: invalid syntax


    When I take out the colon, I get a syntax error in my if statement, again saying that the colon is invalid syntax.When I take that out, i get another syntax error saying that


    admin-trs-imac-2:programmingst uff yandong$ python factors.py
    File "factors.py ", line 5
    x += 1
    ^
    SyntaxError: invalid syntax

    Am i doing something majorly wrong? I cannot figure out what.

    (by the way there are indents in the correct places but they do not show up)
    (Also the arrow in the syntax error in the while loop should be pointing to the colon)
    Last edited by bvdet; Oct 30 '11, 02:03 PM. Reason: Add code tags
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Count the parenthesis.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Code:
      if number % loop == 0:
      loop is not declared any where. Also, if this condition is True, it will be an infinite loop, since the value of "x" won't change. Can't tell anything else without code tags (the "#" symbol) at the top of the post box.

      Comment

      • calvin dong
        New Member
        • Oct 2011
        • 8

        #4
        So, what would you recommend in order to fix the syntax error?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          This would fix your syntax error (missing closing parenthesis):while x <= sqrt(int(number )):
          Your while loop could be a for loop similar to this:for i in xrange(2, int(num**0.5)+1 )

          Then if not num%i, add the number i to your factors list.

          raw_input returns a string, so you need to convert number to an integer.

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Hi. Are you looking for *all* factors? Because even if you fix your code you won't find them this way. You'd need to include number/x also to find the factors that are bigger than sqrt(number).

            I think sympy has some functions to find all factors.

            Comment

            • dwblas
              Recognized Expert Contributor
              • May 2008
              • 626

              #7
              Any factors larger than the square root will have a corresponding factor less than. For example, 20 and 5 are factors of 100, but 20 was found when 5 was tested, so anything greater than 10 is redundant.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Hey Glenton. It's been a while. Glad to have you back.

                This will compile the factors up to num**0.5:
                Code:
                factors = [i for i in xrange(2, int(num**0.5)+1) if not num%i]
                This will extend the list the rest of the way:
                Code:
                factors.extend([num/outList[i] for i in range(len(outList)-1, -1, -1)])
                If the last number in factors was the sqrt of num:
                Code:
                factors.extend([num/outList[i] for i in range(len(outList)-2, -1, -1)])
                Anyone have a better way?

                Comment

                • Glenton
                  Recognized Expert Contributor
                  • Nov 2008
                  • 391

                  #9
                  Thanks, bvdet. It has been a while. Too little python in my life at the moment...

                  If you're allowed to use numpy you could use a mask. It's more brute force in the sense that it just checks everything:
                  Code:
                  import number
                  factors=numpy.arange(2,n+1)
                  factors[n%factors==0]
                  where n is the number.

                  if you want to be a bit more clever (as per OP) you could do this:
                  Code:
                  import numpy
                  factors=numpy.arange(2,int(n**0.5)+1)
                  f1=factors[n%factors==0]
                  s=set(f1).union(set(n/f1))

                  Comment

                  • calvin dong
                    New Member
                    • Oct 2011
                    • 8

                    #10
                    i still get an error(when adding the extra parenthese at the end of sqrt in my while loop) it says that "sqrt" is not defined. I used bvdet's code exactly.(His first revision)

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      You need this:from math import sqrt

                      Comment

                      • calvin dong
                        New Member
                        • Oct 2011
                        • 8

                        #12
                        I changed my code, but for some reason it still doesn't work. There isn't an error displayed, just nothing happens.

                        Code:
                        number = int(input(" Enter a number "))
                        x = 1
                        coollist = []
                        while x <= pow(int(number),0.5):
                            if (number % x) == 0:
                                coollist.append(x)
                            else:
                                x += 1
                        coollist.append(number)
                        print coollist

                        Comment

                        • Glenton
                          Recognized Expert Contributor
                          • Nov 2008
                          • 391

                          #13
                          How are you running your script?

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #14
                            In the while loop, if (number % x) == 0 is True, x never increments and your loop will never end.

                            Comment

                            • calvin dong
                              New Member
                              • Oct 2011
                              • 8

                              #15
                              I feel stupid for not having caught this obvious error...thank you all.

                              Comment

                              Working...