Prime problem

This is the place for queries that don't fit in any of the other categories.

Prime problem

Postby Vendelo » Wed Sep 11, 2013 4:17 pm


I'm pretty new to Python, so for fun and exercise I've been trying to write a procedure in which I input a number, and output every prime number in the interval [0:n].
I managed to do it pretty quickly by first defining a procedure that recognises and returns primes but later on I realised that I wanted two outputs so that if the input number was prime, I'd get True , n and if it wasn't I'd get False , 0.

But now, whenever I run the procedure that gives me all the primes it always ends the list with "None" and I can't figure out why!

Heres the definitions:

Code: Select all
def round_number(n):
    num = n + 0.5
    return int(str(num)[:str(num).find('.')])

def prime(n):
    i = 1
    while i <= n:
        i = i + 1
        if n / (i * 1.0) == round_number(n / (i * 1.0)):
    if n / i != 1:
        return False , 0
        return n / i == 1 , n
def every_prime(n):
    i = 2
    while i <= n:
        is_it_prime , the_prime = prime(i)
        if is_it_prime == True:
            print the_prime
        i = i + 1

Thank you for your help :)
Posts: 2
Joined: Wed Sep 11, 2013 4:10 pm

Re: Prime problem

Postby metulburr » Wed Sep 11, 2013 4:29 pm

You did not provide the function call that you made to execute every_prime(). So i am assuming that the time you did not get None, you just executed the function, and the time you got None, you printed the call of the function.

every function in python returns None if you do not return something, and the print captures what is being returned and prints it to the screen.
we will be moving to on October 1 2016
more details here
User avatar
Posts: 2231
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Prime problem

Postby Vendelo » Wed Sep 11, 2013 4:38 pm

I am really stupid, thank you!!
All day I've been typing print every_prime(n) not knowing what I did wrong!

Thanks again :)
Posts: 2
Joined: Wed Sep 11, 2013 4:10 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 9 guests