Prime code [HELP]

A forum for general discussion of the Python programming language.

Prime code [HELP]

Postby GabrielSilva » Wed May 22, 2013 4:36 pm

Code: Select all
def prime(x):
    while c<x:
        while d%y!=0:
            y = y + 1
        if d == y:
            k = k.append(d)
            c = c + 1
        d = d + 1
    return k   

I'm trying to write the x first prime numbers, where I'm incorrect in this code ?
And... what is the diference between "return" and "print" ? I still don't noticed any difference...
Posts: 8
Joined: Mon May 20, 2013 12:20 am

Re: Prime code [HELP]

Postby hansn » Wed May 22, 2013 5:50 pm

print just prints the output to your terminal without any side effects. with the return statement your function returns a value that you can for example put in a variable.

Code: Select all
def return_primes (limit):
    primes = [2]
    for x in range(3, limit+1, 2):
        is_prime = True
        for i in range(3, int (x**0.5+1.5)):
            if x % i == 0:
                is_prime = False
        if is_prime:
            primes.append (x)
    return primes

var = return_primes (1000) # var would be None without the return statement

coding on phone is not fun
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests