## Prime problem

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

### Prime problem

Hi!

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)):            break    if n / i != 1:        return False , 0    else:        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`

Vendelo

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

### Re: Prime problem

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.

version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr

metulburr

Posts: 835
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Prime problem

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

Thanks again
Vendelo

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