Is Prime Practice Makes Perfect


#1




Oops, try again. Your function fails on is_prime(3). It returns False when it should return True.


I expect a 3 to move through x==2, and x<2 then enter the for loop. It when then have one cycle becasue range( 2,x-1) would be (2,2) and the condition would be 3%2=1 which would not satisfy that condition, so it should move to the else statement where cond=True and then return True ...but apparently I am wrong.....


def is_prime(x):
    cond = False
    if x == 2:
        cond = True
    elif x < 2:
        cond = False
    for n in range(2,x-1):
        if x % n == 0:
            cond = False    
        else:
            cond = True
    return cond


#2

Your for loop runs after the first elif. Move it within if that is your intention, and that should fix it.


#3

If I put in the elif wouldn't it just run if x <2 ? I want it to run for everything greater than two. I thought there may be a problem with the hierarchy of the if statements so I also tried this but I still I got the same error

def is_prime(x):
    cond = False
    if x == 2:
        cond = True
    elif x < 2:
        cond = False
    else:
        for n in range(2,x-1):
            if x % n == 0:
                cond = False   
            else:
                cond = True
    return cond

#4

oops lost indentation - how do I paste that?


#5

This will return True for is_prime(9). Can you figure out why?


#6

Check the Tips section of Community for How to format code samples in topics and replies.

In brief, locate the back-tick on your keyboard. It looks like this:

`

and is usually on the same key as the tilde (~) character, beside the numeral 1 key (just above the Tab key).

When writing a phrase of code inline in prose, denote the code segment by opening and closing with a single back-tick.

This is an example of writing a print statement. Note the code-like characters? That's because it is enclosed in single back-ticks.

When pasting in a block of code. start with a blank line, type three backticks and the lowecase name of the language, python. Enter to start a new blank line, paste in your code, and then hit Enter at the end and type in three more back-ticks to close it.

```python

# your code goes here

```

New forum topics open with a premade template. Each part indicates what to paste or write in that segment.

  • the link to the exercise
  • error message(s)
  • your question or problem and what you expect
  • your code in the section denoted (remove the line indicated when you paste in your code).

#7

Thanks for the pasting info


#8

Oh will it cycle through 9/4, 9/5, etc. and go to true?


#9

Will a break after false fix that because once it is false for one it satisfies the not prime condition?


#10

Nope - it still fails on 3.


#11

Got it to work by adding
if x =3:
cond = True

It doesn't seem very "graceful" though......seems like there is a much better way.


#12

No, it won't get that far. When it is divided by 2 it will go the True. There should not be an else: branch in that if statement. Just the if ___:

which should have raised an exception. The = token is for assignment, not identity comparison (equal in type and value). Use == for this purpose.

It is overkill, though, so you are right in doubting its 'gracefulness'. Any time that question comes up your mind is a good signal that more study, examination of the problem, and closer look at expected outcomes are needed. Never doubt your intuition. It's telling you something.

By the same token, what will happen when you test 7, or 5? They will come back as True, but for the wrong reason... Being ejected by the loop, rather than falling through it by never being ejected.

What happens when cond = True initially? What will return from this basic setup:

if x < 2:
    cond = False
else:
    cond = True
if x > 2:
    for n in range(2, x):
        if x % n == 0:
            cond = False
            break

return cond

#13

Sorry I did use x==3 but did not type it that way here. THe code you typed will default to True and stay True if it there is a remainder in the division. It will change to false if there is no remainder and end the program. I am not sure if it will go to the the return after that break.... does break end the whole program or does it just break from the loop?


#14

It will and does. break terminates the loop and flow continues to the next line after, return.


#15

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.