By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

Syntax error in a while loop

P: 8
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.

Expand|Select|Wrap|Line Numbers
  1. number = raw_input(" Enter a number ")
  2. x = 2
  3. coollist = []
  4. while x <= sqrt(int(number):
  5.     if number % loop == 0:
  6.         coollist.append(x)
  7.     else:
  8.         x += 1
  9. print coollist
The syntax error reads


admin-trs-imac-2:programmingstuff 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:programmingstuff 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)
Oct 30 '11 #1

✓ answered by bvdet

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.

Share this Question
Share on Google+
14 Replies


Expert 100+
P: 621
Count the parenthesis.
Oct 30 '11 #2

Expert 100+
P: 621
Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 30 '11 #3

P: 8
So, what would you recommend in order to fix the syntax error?
Oct 30 '11 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Oct 30 '11 #5

Expert 100+
P: 391
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.
Oct 31 '11 #6

Expert 100+
P: 621
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.
Oct 31 '11 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Hey Glenton. It's been a while. Glad to have you back.

This will compile the factors up to num**0.5:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. factors.extend([num/outList[i] for i in range(len(outList)-2, -1, -1)])
Anyone have a better way?
Oct 31 '11 #8

Expert 100+
P: 391
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:
Expand|Select|Wrap|Line Numbers
  1. import number
  2. factors=numpy.arange(2,n+1)
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. import numpy
  2. factors=numpy.arange(2,int(n**0.5)+1)
  3. f1=factors[n%factors==0]
  4. s=set(f1).union(set(n/f1))
Nov 1 '11 #9

P: 8
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)
Nov 5 '11 #10

bvdet
Expert Mod 2.5K+
P: 2,851
You need this:from math import sqrt
Nov 6 '11 #11

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

Expand|Select|Wrap|Line Numbers
  1. number = int(input(" Enter a number "))
  2. x = 1
  3. coollist = []
  4. while x <= pow(int(number),0.5):
  5.     if (number % x) == 0:
  6.         coollist.append(x)
  7.     else:
  8.         x += 1
  9. coollist.append(number)
  10. print coollist
  11.  
Nov 7 '11 #12

Expert 100+
P: 391
How are you running your script?
Nov 7 '11 #13

bvdet
Expert Mod 2.5K+
P: 2,851
In the while loop, if (number % x) == 0 is True, x never increments and your loop will never end.
Nov 7 '11 #14

P: 8
I feel stupid for not having caught this obvious error...thank you all.
Nov 7 '11 #15

Post your reply

Sign in to post your reply or Sign up for a free account.