#1

My code determines if the number is prime or not. However, the issue is that the final else statement at the bottom will always print out PRIME because of the way my code is written. In other words, can someone please fix the final else and if statements? I know what I wrote is confusing, but it will make sense when you run it and see the issue for yourself.

``````def is_prime(x):
if x ==2:
print ("This is a prime number!")
elif x ==1:
print ("This is not a prime number!")
elif x % 2 == 0:
print ('It is not a prime number')
else:
for i in range(2,x):
i = x / i
if i == int(i):
print ("This is not a prime number")
break
else:
print("This is a prime number")

is_prime(25)``````

#2

After observing your code, I found that the problem lies in the positioning of the last if and else statement
i.e. Whenever you input a number greater than 2 then it is giving a statement

" This is not a prime number"

Proper positioning of your last if-else statement will make it correct

#3

every time your if condition evaluates to false, the else runs

this is not right, for a number to be a prime number the whole loop should run without printing This is not a prime number.

so, you want to move this line outside the loop, so that is only reached after the loop and not every time if condition evaluate to false

#4

Hey there!
Here is my new code:

def is_prime(x):
if x ==2:
print ("This is a prime number!")
elif x ==1:
print ("This is not a prime number!")
elif x % 2 == 0:
print ('It is not a prime number')
else:
for i in range(2,x):
i = x / i
if i == int(i):
print('Not Prime')

``````    else:
print("This is a prime")
print (i)``````

is_prime(25)

Now what makes my code not work is this simple issue. When you print i inside of the for loop, it iterates up to x ( in this case 25) and prints all numbers of i. However, when printing i outside of the loop, it only prints the last number. I know what I wrote is confusing, but you kinda have to see it happen. That's why I inserted a print statement with the else statement. You'll see how it prints only the last number from all the numbers of i.

Anyways, by putting the else statement outside of the loop, it still prints 'This is a prime' because it' still ignoring the fact that there's an integer.

I know what I said is confusing so I apologise. Let me know if you're confused on what I said. Otherwise, thanks in advance for your help

#5

Thank you for your help. Issue is, if I place the if/else statements it won't run properly because it won't iterate through all items of i. Can you please tell me where I should place the if/else statements? Thanks

#6

yes, but our function needs to return True for prime numbers and return False if its not a prime number

when a return keyword is reached, the function will end. So then, the else which you placed outside your loop will only be reached when the number is a prime number (if its not, return False will end the function at some point during the loop when if evaluates to true)

#7

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