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???
#1
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
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(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))
Reply
#4
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?
Reply
#5
(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.
Reply
#6
This link may help you: https://www.tutorialspoint.com/How-to-ge...ing-Python
Reply
#7
I suspect this may be a language issue.
Prime/Not prime or even/uneven(odd)
Because 15 is not "prime".
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#8
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".
Reply
#9
(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
Reply
#10
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.
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 485 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 864 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,568 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,265 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,795 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,565 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,535 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,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,515 Jun-09-2021, 10:01 AM
Last Post: topfox
  Can somebody check what is wrong with my code? hplus_liberation 4 2,614 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