Python Forum
What is wrong with my code???
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with my code???
#11
(Sep-16-2020, 11:54 AM)ebolisa Wrote: Your code fails!!

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


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

It was to check Prime/Not-Prime.

Thanks for the support though.
Reply
#12
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))
Reply
#13
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.
Reply
#14
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))
Reply
#15
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#16
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 787 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,469 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,180 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,681 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,387 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,487 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,602 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,428 Jun-09-2021, 10:01 AM
Last Post: topfox
  Can somebody check what is wrong with my code? hplus_liberation 4 2,545 Sep-16-2020, 05:52 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020