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


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

(Sep-16-2020, 11:54 AM)ebolisa Wrote: 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.

It was to check Prime/Not-Prime.

Thanks for the support though.


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

def prime_num(a):
    counter=1
    if a>1:
        for x in range(2,a):
            if(a%x)==0:
                print("The number is NOT PRIME")
                counter=0
                break
        if(counter==1):
             print("The number is PRIME")
    else:
        print("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-16-2020

You'll still be printing None. Since your function has no return statement, it implicitly returns None, which you're printing on lines 14-18. Either:

1. Return the strings from the function and leave those lines as they are or

2. Remove the calls to print on those lines.


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

def prime_num(a):
    counter=1
    if a>1:
        for x in range(2,a):
            if(a%x)==0:
                return ("The number is NOT PRIME")
                counter=0
                break
        if(counter==1):
             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))



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

You could consider more general approach to this function.

If objective is to determine whether number is prime or not then the answer can be yes or no which translates into Python as True or False. Of course, depending on specifics False may not be good enough (if for some reason distinction must be made between not primes and not valid numbers) but this is validation problem which should/could be dealt separately.

As for solution there is no need to test even numbers after 2 (if number doesn't divide with 2 then it is odd and will not divide with 4, 6, 8...). But.... you should also keep in mind that there is one even number among primes: 2. Upper limit should be not number itself but square root of the number which primality is to be tested. These are low hanging fruits which significantly reduce calculations needed.


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

(Sep-16-2020, 01:40 PM)perfringo Wrote: You could consider more general approach to this function.

If objective is to determine whether number is prime or not then the answer can be yes or no which translates into Python as True or False. Of course, depending on specifics False may not be good enough (if for some reason distinction must be made between not primes and not valid numbers) but this is validation problem which should/could be dealt separately.

As for solution there is no need to test even numbers after 2 (if number doesn't divide with 2 then it is odd and will not divide with 4, 6, 8...). But.... you should also keep in mind that there is one even number among primes: 2. Upper limit should be not number itself but square root of the number which primality is to be tested. These are low hanging fruits which significantly reduce calculations needed.

Well said. Thanks for the details. It would help me surely in future.