You are viewing our Forum Archives. To view or take place in current topics click here.
Python help needed!
Posted:

Python help needed!Posted:

R8-
  • V5 Launch
Status: Offline
Joined: Oct 25, 201113Year Member
Posts: 340
Reputation Power: 29
Status: Offline
Joined: Oct 25, 201113Year Member
Posts: 340
Reputation Power: 29
So i need help with creating the code for a prime number counter. It is supposed to use the input from the user and display every number that is prime from what the user has used. Such as if the user puts 7 it displays 5, and 3, etc.

Here is the code i am supposed to build on. And i am using the codeacademy labs website to create the code
#This line of code defines your function that will contain your code to count all the prime numbers (don't change this)
def primeCounter():

#Put your code to ask for users input here and store it in a variable

#Create a for loop that will count from 'i' up to the number inputted by the user here


#Write a conditional if statement that checks if the index is a prime number and prints if it is
#Hint: Remember that the index variable 'i' starts with the initial value of 0 and increments by 1

#This calls the function primeCounter() and runs the program (don't change this)
primeCounter()


Please help me
#2. Posted:
r00t
  • Administrator
Status: Offline
Joined: May 18, 201113Year Member
Posts: 16,423
Reputation Power: 24471
Status: Offline
Joined: May 18, 201113Year Member
Posts: 16,423
Reputation Power: 24471
The idea is that you do it yourself, so this should help you get started.

I would start by making a function that checks whether a number is prime. Call it "isPrime(num)". You can write it with the following assumptions:

    Prime numbers are positive.
    0 and 1 are not prime.
    2 is the only even prime number.
    Any odd number from 3 to the square root of the number could be a factor of the number if it is not prime, so you'll want to check those.
    If a factor is found, there's no need to check any more possible factors.
#3. Posted:
R8-
  • 2 Million
Status: Offline
Joined: Oct 25, 201113Year Member
Posts: 340
Reputation Power: 29
Status: Offline
Joined: Oct 25, 201113Year Member
Posts: 340
Reputation Power: 29
r00t wrote The idea is that you do it yourself, so this should help you get started.

I would start by making a function that checks whether a number is prime. Call it "isPrime(num)". You can write it with the following assumptions:

    Prime numbers are positive.
    0 and 1 are not prime.
    2 is the only even prime number.
    Any odd number from 3 to the square root of the number could be a factor of the number if it is not prime, so you'll want to check those.
    If a factor is found, there's no need to check any more possible factors.

Thank you! Actually helped me finish it.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.