Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (anonymous):

I am trying to solve this question " Create a program to count by prime numbers. Ask the user to input a number, then print each prime number up to that number." And I wrote the following code: number = int (raw_input ("Enter a number and the program will give you a list of prime numbers upto that number: ")) for item in range (2, number): x = 1 or item y = int (y) y != x if item%y == 0: break else: print item It says y is not defined. Actually, I want to set y as "any other integer than 1 or the item in the range (2, number) ". Please help!

OpenStudy (anonymous):

x=1 or item what are you trying to do here? and if I get you right i don't think you can do such thing with y I understood you algorithm, what you trying to do but i don't think it's possible, you should thing another way of calculating primes

OpenStudy (anonymous):

thank you for replying. Prime numbers aren't divisible by any other numbers than '1' or 'themselves'. So before writing the argument item%y, I wanted to assign y as an integer which is neither '1' nor the item being divided. To do this I set x = 1 or item (i.e. the item being dealt with in every loop in the range (2, 100)). Then, I set y != x. But this gave me an error saying y is not defined. Everything else seems to be ok though. I think "y = int y" or something like that works in java. All i want to do is set y as an integer which is neither 1 nor the item(being dealt with) in the range.

OpenStudy (anonymous):

Try initializing y outside of the for loop

OpenStudy (anonymous):

As a simple exercise to get started, consider writing an odd number generator and then add to that. e.g. >>> for i in range(3,30): ... if i%2 != 0: ... oddlist.append(i) ... >>> print oddlist [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29] Also, you don't need to worry about setting data types.

OpenStudy (anonymous):

or use a second loop for divisors print 1, 2, 3 for number in the range of five to user input: for divisor in the range of two to number: if number mod divisor equals zero: number is not prime stop testing number if number is prime: print number

OpenStudy (anonymous):

Thankyou all for replying! @abishop: I tried y= int(y) outside the loop. Still it says y is not defined. @mkorangestripe: For odd numbers it is quite easy, but things get a bit trickier for prime nos. @bwCA: I wrote the following code as per your instructions: print 1, 2, 3 input = int (raw_input ("Give a number: ")) for number in range (5, input): for divisor in range (2, number): if number%divisor == 0: break else: print number (hope i wrote it correctly) But i get numbers like 9, 15, 21.... along with prime numbers in the result.

OpenStudy (anonymous):

if you just want the answer, here it is http://dpaste.com/663809/

OpenStudy (anonymous):

thank you so much! it works :)

OpenStudy (anonymous):

how can you do the same in C syntax

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!