I am working on Problem set 1, #1: trying to find the primes. not sure why the for loop isn't working as intended. help and input would be greatly appreciated! here is the pastebin of my program: http://pastebin.com/99vNU8jG thanks in advance! :)
If what you want with the for loop is go through each value of the list you should do the following: for j in primelist:
It would help you to separate out the check if prime from making the list. These really are two different functions . By doing that you can test just the prime test alone and make sure it works before iterating through the number set. def prime_test(number): do something return True or False then after that is done and error free make the loop to make a list def make_list(number): while i < number: if prime_test(i) == True: do something else: do something else
line 30 of your post indicates that you are adding things to primelist when you shouldn't? where, in the code, do you add things to primelist? your if/else statement at lines 15 and 19 are saying if the number isn't evenly divisible by This prime do to the next number else add the number to the primelist you shouldn't add the number to your list til After it has been checked by All the primes in primelist the stuff in the else clause (line 19) is in the wrong place
Thanks for all your guys' help! it's been very helpful. I finally managed to create a successful prime number counting program. :)
style comments - my opinion only: If you think about how you are going to use your functions and have them return what you need and name them appropriately, the code that calls them can be very 'readable' and won't need any comments. - maybe think ahead about how you will call them, not just how they will work. In this case it seems (to me) that you want your function to tell you if a number is prime or not so maybe it should return either True or False. Then if you give it a name like is_prime, you can write a conditional like - if is_prime(x): do something http://pastebin.com/B3je09eR
@bwCA: At first I tried to have the function return a "true or false" if it was a prime, but I could not figure out how to do that since I needed to check every number in the primelist. When i wrote that function, it would include every number. I think the counting system is much better because it counts the number of times a number is divisible by a number in the primelist.
in line 15 you basically do a true / false check - either primecheck returns zero or something other than zero -
Join our real-time social learning platform and learn together with your friends!