Hey! I'm still stuck on the first problem(a prime number test)...my program makes sense to me, but it's not right because it doesn't work at all...help? :D #primetest x = int(raw_input('prmetest')) if x<2: print 'invalid input' elif x==2: print 'prime2' if x>2: a = int(x**.5)+1 for n in range(3,a,2): if x%n == 0: print 'Nope' else: print 'YUP'
For starters I think the question is asking for the 1000th prime specifically, so no raw_input() is neccessary. There are many ways to solve this problem (you will see that if you google for help). I just completed it using a while loop, a statement using for variable in range(x,y,) and the % == 0 test that you used.
Oh. But what IF I wanted to do a full-on prime test? Then were would the error be?
Hmm, i think the statement after elif should be 'else' instead of another 'if'. also, you are saying int(x**.5)+1, which is the squareroot of x (I think). I'm brand new to this. I've been doing "python the hard way" on the side of this course and it helps a lot. You should check it out.
Program still doesn't work with replacing the last if with 'else'. Hmm...maybe other ideas? Thanks, ScottKampsDuac, though.
I think if your 'yup' is a 'yes, it's a prime' and 'nope' is a 'no, it's not a prime', they are are backwards. You want a remainder if it's prime. Also watch out if you test the number 5, your range won't work here.
Yeah, 'yup' means prime. But if x%n == 0: print 'Nope' means that if there is no remainder, it's composite. So I don't think it is backwards. As for the number five, should I just add an if x=5; print 'prime'? or is there another way. The program still gives me a syntax error--would anybody know why?
Sorry--you're right. Did you try putting in 'break' after x%n == 0...that way the loop stops when composite is found
hmmmmm...thank you, I never knew that command. But something is still funny...because it hasn't been outputting 'YUP' or 'Nope' at all...
Did you change your code to else: a = int(x**.5) + 1 for n in range(3,a,2):......? You might want to stick in a few print statements to find out why its skipping your if statement. You might also want to use something like pastebin or dpaste to make it easier to read your code. You can also attach files...
Thank you...it's working now.
Join our real-time social learning platform and learn together with your friends!