-6

I have code that checks if the number is prime or note

when I run it gives me an error

my code:

a = int (input("your numbre :"))
b = range(1,a+1)
if a%b == 0:
    for i in b :
        if b == a or b == 1:
            print("the numbre you input is prim number")
else:
    print("the nmubre you input is note prem numbre")

the error generated :

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\chap8ex2.py", line 3, in <module>
    if a%b == 0:
TypeError: unsupported operand type(s) for %: 'int' and 'range'
zerocool
  • 2,715
  • 2
  • 17
  • 33

2 Answers2

1

The range function creates an array of numbers between its parameters. For example:

>>> range(1, 4)
=> [1, 2, 3]

In your code you're saying a%b which is finding the remainder between a number the user inputted and a range object. Think of the range object as a python list. So what you're doing is 5 % [1, 2, 3] that doesn't make sense. Check this separate thread to find out how to implement a prime number checker in python. Python Prime number checker

Community
  • 1
  • 1
Dor-Ron
  • 2,283
  • 2
  • 11
  • 24
0
a = int (input("your numbre :"))
b = range(1,a+1)
for i in b :
    if b == a or b == 1:
        if a%b!=0:
            print("the number you input is prim number")
else:
    print("the number you input is not a prime number")