0

I'm trying to build a simple Prime Number checker in Python 3.x and I'm running into some issues. I will post my code and then explain my difficulties.

number = input("Please enter a number: ")

is_prime = True;

for factor in range(2, number):
    if number % factor == 0:
        is_prime = False;

if is_prime == True:
    print("%d is a prime number!") % number
else:
    print ("%d is NOT a prime number!") % number 

Now when I run the following code, I get this error:

C:\Users\clark\Documents\Python Projects>python PrimeNumberChecker.py Please enter a number: 4 Traceback (most recent call last): File "PrimeNumberChecker.py", line 5, in for factor in range(2, number): TypeError: 'str' object cannot be interpreted as an integer

Now, from my limited understanding of Python the input method that I'm using to evaluate the number variable should return an integer so I'm not sure why it's telling me there's a conversion issue. Could anybody shed some light on what's going on here? I'm very new to Python.

Thanks

ettanany
  • 19,038
  • 9
  • 47
  • 63
  • You need to cast the input you get to a number. – TZHX Dec 03 '16 at 11:20
  • I'm not sure what you mean by that. Could you please elaborate? –  Dec 03 '16 at 11:21
  • Your number variable, is a string. Cast it to an int. read a basic tutorial on getting user input in python. – TZHX Dec 03 '16 at 11:22
  • 1
    BTW, you can make your primality test a _lot_ more efficient. For starters, as soon as you've determined that `number` has a factor you can break out of the loop. Also, there's no need to test all possible factors up to `number` because if `number` isn't prime then at least one of its factors must be <= `sqrt(number)`. Can you see why? – PM 2Ring Dec 03 '16 at 11:37
  • I see what you're saying yes, thank you! –  Dec 03 '16 at 23:32

3 Answers3

2

In Python 3.x, you need to convert your variable number to int like this:

number = int(input("Please enter a number: "))

See these two examples from the two version of Python that I have on my machine:

In Python 3.4:

>>> number = input("Please enter a number: ")
Please enter a number: 4
>>> type(number)
<class 'str'>

In Python 2.7:

>>> number = input("Please enter a number: ")
Please enter a number: 4
>>> type(number)
<type 'int'>

Please take a look at this important answer of How can I read inputs as integers in Python?

Community
  • 1
  • 1
ettanany
  • 19,038
  • 9
  • 47
  • 63
0

In python2.x

number = input("Please enter a number: ")  

number will be a int. But in python3.x it will be str. You are using python.3x so you have to convert that into integer. with using int.

number = int(number)

Might be you are referenced a code which is wrote in python2.x

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

You could try this one

number=int(input("please enter a number")

counter=0

for factor in range (1,number):
     if number%factor==0:
        counter=counter+1

if counter==2:
  print(number,"is prime")
else:
  print(number,"is not prime")