-3
print ("Prime number tester");
number = input("Enter number: ");
x = 2;
y = 0;

while (x < number):
    if number % x == 0:
        y = y + 1;
        x = x + 1;
    else:
        x = x + 1;



if (y == 0):
    print (number, "is prime.");
else:
    print (number, "isn't prime.");
input();

Why does this crash after I type in the number? Please help as I am new and have no idea why?

Magna Wise
  • 49
  • 5

1 Answers1

2

input() in Python 3 returns a string. You cannot compare numbers to strings:

>>> '10' < 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

Convert the input to an integer first:

number = int(input("Enter number: "))

If the user did not enter a valid number, the int() call raises a ValueError. Depending on how much error handling you want to do, you probably want to catch that exception. See Asking the user for input until they give a valid response for more information on how that'd work.

Python doesn't need those ; semicolons; you can safely remove them from your code.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343