Note that there are some explanatory texts on larger screens.

plurals
  1. POPython and Prime Numbers
    text
    copied!<p>I know there are a bunch of topics on this, and I hate to create a new one, but none of the topics seem to fit exactly right, unless it is doing something in a more advanced way. I am in an intro to programming class, and want (need?) to use simple commands that we've learned in the first week to do this, and I also want to have code that is "my own" and not just snag something off the internet.</p> <p>I am working on two scripts (.py files) that are extremely basic. One asks for a number and prints out whether it is a prime number or not, the other prints out all of the prime numbers up to the 1000th prime number (Which we know is 7919, because we were given a link to a list of primes to check our work).</p> <p>My first script:</p> <pre><code>yourNumber= input('Enter a number: ') isPrime = 0 if yourNumber == 1: isPrime = 1 print(yourNumber,'is not a prime number') else : for primeCheck in range (int(2), int(yourNumber)): if int(yourNumber)%int(primeCheck) == 0: isPrime = 1 break else: isPrime = 0 if isPrime == 0: print(yourNumber,'is a prime number') else: print(yourNumber,'is not a prime number') </code></pre> <p>This one seems to work for any number except 0 and 1. I don't know what I am doing wrong.</p> <hr> <p>My second script:</p> <pre><code>numberPrimes = 0 startNumber = 0 currentNumber = startNumber isPrime = 0 while numberPrimes &lt; 1000: if currentNumber == 1: currentNumber = currentNumber + 1 else: for primeCheck in range (int(2), int(currentNumber)): if int(currentNumber)%primeCheck == 0: isPrime = 1 break else: isPrime = 0 if isPrime == 0: print(currentNumber, 'is prime') numberPrimes = numberPrimes + 1 currentNumber == currentNumber + 1 else: currentNumber == currentNumber + 1 </code></pre> <p>This one basically spits out "0 is prime" one thousand times. Somehow the currentNumber is not going up or something. I'm not sure.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload