Kinda newb-ish question

C

ChocoboMog123

What's wrong with line 8 in this code?
x=1
while 1==1:
x=x+1
y=range(1,x)
z=0
q=9
for count in y:
q=x%y
if q==0:
z=z+1
if z<1:
print x
It keeps giving me
Traceback (most recent call last):
File "C:\Python23\Prime Number.py", line 8, in -toplevel-
q=x%y
TypeError: unsupported operand type(s) for %: 'int' and 'list'

It's supposed to be a program to print out prime numbers.
If anyone knows of a program that does this or can write one, can you
show me the code? Thanks.
 
H

Hans Nowak

ChocoboMog123 said:
What's wrong with line 8 in this code?
x=1
while 1==1:
x=x+1
y=range(1,x)
z=0
q=9
for count in y:
q=x%y
if q==0:
z=z+1
if z<1:
print x
It keeps giving me
Traceback (most recent call last):
File "C:\Python23\Prime Number.py", line 8, in -toplevel-
q=x%y
TypeError: unsupported operand type(s) for %: 'int' and 'list'

The error message points out what's wrong with it... you're using the %
operator with an int (x) and a list (y), which is unsupported. You probably
mean something like x % count, although that doesn't make the algorithm work.

Quick tips: use range(2,x) rather than range(1,x), and x % count. An infinite
loop is probably not a good idea either. :)

HTH,
 
D

Duncan Smith

ChocoboMog123 said:
What's wrong with line 8 in this code?
x=1
while 1==1:
x=x+1
y=range(1,x)
z=0
q=9
for count in y:
q=x%y
if q==0:
z=z+1
if z<1:
print x
It keeps giving me
Traceback (most recent call last):
File "C:\Python23\Prime Number.py", line 8, in -toplevel-
q=x%y
TypeError: unsupported operand type(s) for %: 'int' and 'list'

The clue is in the traceback. 'y' is a list. Presumably you meant q = x %
count.

Duncan
 
B

BW Glitch

ChocoboMog123 said:
What's wrong with line 8 in this code?
x=1
while 1==1:
I think it would be better as:
while True:
x=x+1
y=range(1,x)
z=0
q=9
for count in y:
q=x%y
It should be:
q=x%count
if q==0:
z=z+1
if z<1:
print x
It keeps giving me
Traceback (most recent call last):
File "C:\Python23\Prime Number.py", line 8, in -toplevel-
q=x%y
TypeError: unsupported operand type(s) for %: 'int' and 'list'

It's supposed to be a program to print out prime numbers.
If anyone knows of a program that does this or can write one, can you
show me the code? Thanks.

As it points out, y is a list.


--
Glitch

-----BEGIN TF FAN CODE BLOCK-----
G+++ G1 G2+ BW++++ MW++ BM+ Rid+ Arm-- FR+ FW-
#3 D+ ADA N++ W OQP MUSH- BC- CN++ OM P75
-----END TF FAN CODE BLOCK-----

"So, give us the scoop! What's it actually feel like, bein' a Pred?"
"Like you're three gigabytes of attitude on a two-gig hard drive. No
wonder they got personality problems!"
-- Cheetor and Rhinox, "Dark Designs"
 
M

Mark Dufour

What's wrong with line 8 in this code?
x=1
while 1==1:
x=x+1
y=range(1,x)
z=0
q=9
for count in y:
q=x%y
if q==0:
z=z+1
if z<1:
print x
It keeps giving me
Traceback (most recent call last):
File "C:\Python23\Prime Number.py", line 8, in -toplevel-
q=x%y
TypeError: unsupported operand type(s) for %: 'int' and 'list'

It's supposed to be a program to print out prime numbers.
If anyone knows of a program that does this or can write one, can you
show me the code? Thanks.

Sure. The expression range(1,x) produces a list, not a number. So y is a list,
and x%y now tries to perform a modulo operation with a list as modulo number!

Instead of using z, you might also use a boolean, or True/False variable,
perhaps like this:

x=1

while 1:
x += 1
prime = True
for y in range(2,x):
if x%y == 0:
prime = False
break
if prime: print x

Notice the (2,x) instead of (1,x). You don't want to test for division by 1..

Of course, finding primes this way by trial division is a very slow method..
A simple optimization would be to only try numbers up to the square root of
x.


Mark Dufour.
 
D

David M. Wilson

H

Harald Massa

File "C:\Python23\Prime Number.py", line 8, in -toplevel-
An infinite loop is probably not a good idea either. :)

Why should it not be a good idea? Are there any ideas that the number of
primes is limited?


:)

Harald
 
H

Hans Nowak

Harald said:
File "C:\Python23\Prime Number.py", line 8, in -toplevel-



Why should it not be a good idea? Are there any ideas that the number of
primes is limited?

:)

:) No, but the program will never finish, which doesn't make it very useful
except for staring at its output while eating tons of pretzels and listening to
The Orb.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top