# create a function which tells if a number is prime

• (2 Pages)
• 1
• 2

## 15 Replies - 1054 Views - Last Post: 26 August 2014 - 06:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=350782&amp;s=f23d3168c369ad9dfd5701902dde5863&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Letoatreides3508

Reputation: -4
• Posts: 15
• Joined: 21-July 14

# create a function which tells if a number is prime

Posted 21 July 2014 - 05:38 PM

I'm trying to get my homework done for my python programming class, and the assignment is to create a function which tells me if a number is prime. This is the code I entered:

```def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True

test = 5

is_prime(test)
```

I'm getting an indentation error message, but I can't figure out what isn't being indented correctly. Can someone please tell me where I'm going wrong here?

Is This A Good Question/Topic? 0

## Replies To: create a function which tells if a number is prime

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 5798
• Posts: 28,522
• Joined: 10-May 07

## Re: create a function which tells if a number is prime

Posted 21 July 2014 - 05:51 PM

** Renamed title to be more descriptive than 'help with code' **

### #3 Letoatreides3508

Reputation: -4
• Posts: 15
• Joined: 21-July 14

## Re: create a function which tells if a number is prime

Posted 21 July 2014 - 05:53 PM

Ok... There doesn't seem to be any option to edit the title.. But thanks.

### #4 Letoatreides3508

Reputation: -4
• Posts: 15
• Joined: 21-July 14

## Re: create a function which tells if a number is prime

Posted 21 July 2014 - 05:59 PM

Also, not sure if this would be useful information or not, but the exact message reads 'assertion error'

### #5 jon.kiparsky

• Pancakes!

Reputation: 8472
• Posts: 14,658
• Joined: 19-March 11

## Re: create a function which tells if a number is prime

Posted 21 July 2014 - 06:45 PM

Letoatreides3508, on 21 July 2014 - 07:38 PM, said:

I'm trying to get my homework done for my python programming class, and the assignment is to create a function which tells me if a number is prime. This is the code I entered:

```def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True   <-----This line is outdented outside of the scope of the function declaration.
test = 5

is_prime(test)
```

I'm getting an indentation error message, but I can't figure out what isn't being indented correctly. Can someone please tell me where I'm going wrong here?

### #6 f0ssil

Reputation: 1
• Posts: 8
• Joined: 22-July 14

## Re: create a function which tells if a number is prime

Posted 22 July 2014 - 09:11 AM

The error is because 'return True' is outside the function. Correct implementation would be

```def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
else:
return True

test = 5

is_prime(test)

```

### #7 cfoley

• Cabbage

Reputation: 2155
• Posts: 4,525
• Joined: 11-December 07

## Re: create a function which tells if a number is prime

Posted 22 July 2014 - 09:32 AM

That's not right. How would the loop ever get to 3?

### #8 f0ssil

Reputation: 1
• Posts: 8
• Joined: 22-July 14

## Re: create a function which tells if a number is prime

Posted 22 July 2014 - 11:09 AM

cfoley, on 22 July 2014 - 09:32 AM, said:

That's not right. How would the loop ever get to 3?

My bad, I was just correcting the indentation and not the logic. The logic should be :-

```def is_prime(n):
if n > 1:
for i in range(2, n):
if n % i == 0:
return False
else:
return True
else:
return False

```

P.S - It does work for 3 as well.

### #9 jon.kiparsky

• Pancakes!

Reputation: 8472
• Posts: 14,658
• Joined: 19-March 11

## Re: create a function which tells if a number is prime

Posted 22 July 2014 - 11:15 AM

This loop can never get past 2, because it always returns some value on the first item it sees, which is 2. So this is more of an is_even than an is_prime

I have a blog post about finding primes that you might find useful.

### #10 sepp2k

• D.I.C Lover

Reputation: 2223
• Posts: 3,427
• Joined: 21-June 11

## Re: create a function which tells if a number is prime

Posted 22 July 2014 - 01:48 PM

f0ssil, on 22 July 2014 - 08:09 PM, said:

My bad, I was just correcting the indentation and not the logic.

The original logic was fine (for inputs greater than 1 anyway). You broke it by moving the return true into an else in the loop rather than having it after the loop.

### #11 cfoley

• Cabbage

Reputation: 2155
• Posts: 4,525
• Joined: 11-December 07

## Re: create a function which tells if a number is prime

Posted 22 July 2014 - 02:20 PM

Quote

P.S - It does work for 3 as well.

Aaaah well that is interesting. Your second version gives the correct answer for 1--8. It looks as if all is well but then you ask if 9 is prime and it says "yes".

Jon.kiparsky and sepp2k have explained it well.

### #12 pythonuser007

Reputation: 1
• Posts: 48
• Joined: 25-January 14

## Re: create a function which tells if a number is prime

Posted 22 August 2014 - 12:59 AM

Letoatreides3508, on 21 July 2014 - 05:38 PM, said:

I'm trying to get my homework done for my python programming class, and the assignment is to create a function which tells me if a number is prime. This is the code I entered:

```def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True

test = 5

is_prime(test)
```

I'm getting an indentation error message, but I can't figure out what isn't being indented correctly. Can someone please tell me where I'm going wrong here?

I think the problem is that your not using braces.

```#include <stdio.h>

int check_prime(int prime);

main()
{
int result, prime;
printf("\nEnter a number you want to check for it being a prime number: ");
scanf("%d", &prime);
puts("");
result = check_prime(prime);
if (result == 0)
printf("\nYour number is not a prime number.\n");
else
printf("\nYour number is a prime number.\n");
}

int check_prime(int prime)
{
int loop;
for (loop = 2; loop < prime; loop++)
{
if ((prime % loop) == 0)
{
return 0;
}
}
return 1;
}

```

### #13 jon.kiparsky

• Pancakes!

Reputation: 8472
• Posts: 14,658
• Joined: 19-March 11

## Re: create a function which tells if a number is prime

Posted 22 August 2014 - 06:56 AM

pythonuser007, on 22 August 2014 - 02:59 AM, said:

I think the problem is that your not using braces.

You're aware that we're talking about python, right?

### #14 no2pencil

• Professor Snuggly Pants

Reputation: 5798
• Posts: 28,522
• Joined: 10-May 07

## Re: create a function which tells if a number is prime

Posted 22 August 2014 - 06:59 AM

Python is a snake. Snake starts with a C.

### #15 duanuys

Reputation: 0
• Posts: 11
• Joined: 10-November 08

## Re: create a function which tells if a number is prime

Posted 26 August 2014 - 06:31 AM

```def is_prime(num):
if num % 2 == 0:
return False
else:
return True

number = 9

if is_prime(number):
print("%i is prime" % number)
else:
print('%i is even' % number)
```