473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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.

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.

14 11335
dwblas
626 Expert 512MB
Count the parenthesis.
Oct 30 '11 #2
dwblas
626 Expert 512MB
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
So, what would you recommend in order to fix the syntax error?
Oct 30 '11 #4
bvdet
2,851 Expert Mod 2GB
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
Glenton
391 Expert 256MB
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
dwblas
626 Expert 512MB
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
2,851 Expert Mod 2GB
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
Glenton
391 Expert 256MB
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
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
2,851 Expert Mod 2GB
You need this:from math import sqrt
Nov 6 '11 #11
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
Glenton
391 Expert 256MB
How are you running your script?
Nov 7 '11 #13
bvdet
2,851 Expert Mod 2GB
In the while loop, if (number % x) == 0 is True, x never increments and your loop will never end.
Nov 7 '11 #14
I feel stupid for not having caught this obvious error...thank you all.
Nov 7 '11 #15

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

Similar topics

5
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
2
by: RSH | last post by:
I have a rather simple script that transfers data from a SQL server database to an Access database. The procedure is intended to be dynamic so I am basically adding a datarow and then passing the...
7
by: Dustin MacDonald | last post by:
Hi everyone. This is my first time posting to this newsgroup, and although I maintain my netiquette I might've missed something specific to the newsgroup, so hopefully you can avoid flaming me...
2
by: HalfCoded | last post by:
Hi everyone, I am currently working at learning perl but come up with two problems i can't clear on my own. I use perl version 5.8 on windows xp The complete I am working on is supposed to...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: Stef Mientki | last post by:
hello, I've syntax error which I totally don't understand: ########## mainfile : import test_upframe2 if __name__ == '__main__': var1 = 33 code = 'print var1 + 3 \n'
0
by: Terry Reedy | last post by:
Stef Mientki wrote: Indendation is screwed. Is the above all do_more body? Which locals does this get you? __init__'s? (locals()?) Isn't this just the same as globals()?
3
by: dowlingm815 | last post by:
I am receiving a Do While Syntax Error - Loop without Do - Can't See it. I would appreciate any fresh eyes? Mary Private Sub Create_tblNJDOC() On Error GoTo Err_Hndlr
3
by: lingjun | last post by:
Hi, I am taking my first programing course in college... and I am completely lost on this assignment. I am not sure what is wrong with my current code. Any help will be appreciate it... thanks! ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.