Python Forum
What is wrong with my code??? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: What is wrong with my code??? (/thread-29662.html)

Pages: 1 2


What is wrong with my code??? - MrLeads - Sep-15-2020

Can anyone please help me with the problem with this code-

def prime_num(a):
    if a>1:
     for x in range(2,a):
        if (a%x)==0:
             print("The number is NOT PRIME")
             break
        else:
            print("The number is PRIME")
            break
    else:
        print("Invalid Number")
        

print (prime_num(0))
print (prime_num(5))
print (prime_num(10))
print (prime_num(15))
print (prime_num(20))
I am getting an output like this-

Quote:Invalid Number
None
The number is PRIME
None
The number is NOT PRIME
None
The number is PRIME
None
The number is NOT PRIME
None



RE: What is wrong with my code??? - DPaul - Sep-15-2020

1) You print everything twice, you only need to call the function, not print it.
2) better still: insert a "return" statement into the function with the appropriate text, instead of the prints.
You can format it in a print at the bottom
3) the first division that does not work will declare the number as prime, you might rethink that (line4)

Paul


RE: What is wrong with my code??? - MrLeads - Sep-15-2020

(Sep-15-2020, 10:02 AM)DPaul Wrote: 1) You print everything twice, you only need to call the function, not print it.
2) better still: insert a "return" statement into the function with the appropriate text, instead of the prints.
You can format it in a print at the bottom
3) the first division that does not work will declare the number as prime, you might rethink that (line4)

Paul

def prime_num(a):
    if a>1:
     for x in range(2,a):
        if (a%x)==0:
             return("The number is NOT PRIME")
             break
        else:
            return("The number is PRIME")
            break
    else:
        return("Invalid Number")
        

print (prime_num(0))
print (prime_num(5))
print (prime_num(10))
print (prime_num(15))
print (prime_num(20))



RE: What is wrong with my code??? - ndc85430 - Sep-15-2020

Those break statements are unnecessary as they'll never be reached.

Also, is your logic correct for the else branch? Do you really want to return as soon as you find a value for x that gives no remainder?


RE: What is wrong with my code??? - MrLeads - Sep-15-2020

(Sep-15-2020, 01:28 PM)ndc85430 Wrote: Those break statements are unnecessary as they'll never be reached.

Also, is your logic correct for the else branch? Do you really want to return as soon as you find a value for x that gives no remainder?

Right you are -
def prime_num(a):
    if a>1:
     for x in range(2,a):
        if (a%x)==0:
             return("The number is NOT PRIME")
        else:
            return("The number is PRIME")
    else:
        return("Invalid Number")
        

print (prime_num(0))
print (prime_num(5))
print (prime_num(10))
print (prime_num(15))
print (prime_num(20))
is absolutely fine.

Even tried another way to this -
a=int(input("Enter a number: "))
if a>1:
 for x in range(2,a):
    if (a%x)==0:
         print("The number is NOT PRIME")
         break
    else:
        print("The number is PRIME")
        break
else:
    print("Invalid Number")
Regarding your second query, well this is just to check if the number is prime or not and nothing more than that.

Can you please elaborate what you want to add here?!? That would help us to learn a bit more. Looking forward for your reply.


RE: What is wrong with my code??? - ebolisa - Sep-15-2020

This link may help you: https://www.tutorialspoint.com/How-to-generate-prime-numbers-using-Python


RE: What is wrong with my code??? - DPaul - Sep-15-2020

I suspect this may be a language issue.
Prime/Not prime or even/uneven(odd)
Because 15 is not "prime".
Paul


RE: What is wrong with my code??? - deanhystad - Sep-15-2020

10 % 2 == 0 is proof that 10 is not prime, but 9 % 2 != 0 is not proof that 9 is prime. Yet this is the logic you are using in your code. To determine if a number is prime you must test all possible factors, not just 1.

Since this is probably a homework assignment I will not give you the answer, but I will give you a hint. You do not have to use "else".


RE: What is wrong with my code??? - MrLeads - Sep-16-2020

(Sep-15-2020, 03:48 PM)DPaul Wrote: I suspect this may be a language issue.
Prime/Not prime or even/uneven(odd)
Because 15 is not "prime".
Paul

I got your point here. Yes the program here is actually not finding Prime Number rather it is finding Even & Odd. Cry Cry Cry

(Sep-15-2020, 04:08 PM)deanhystad Wrote: 10 % 2 == 0 is proof that 10 is not prime, but 9 % 2 != 0 is not proof that 9 is prime. Yet this is the logic you are using in your code. To determine if a number is prime you must test all possible factors, not just 1.

Since this is probably a homework assignment I will not give you the answer, but I will give you a hint. You do not have to use "else".


num = int(input("Enter a number: "))

if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
       
else:
   print(num,"is not a prime number")
This is fine I believe


RE: What is wrong with my code??? - ebolisa - Sep-16-2020

Your code fails!!

You may learn some from here https://www.programiz.com/python-programming/examples/odd-even


Nevermind. Your prime/not prime odd/even q was confusing.