Python 为什么程序会崩溃 打印(“质数测试仪”); 数字=输入(“输入数字:”); x=2; y=0; 而(x

Python 为什么程序会崩溃 打印(“质数测试仪”); 数字=输入(“输入数字:”); x=2; y=0; 而(x,python,Python,为什么在我输入号码后会崩溃? 请帮助我,因为我是新手,不知道为什么?input()在Python 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:

为什么在我输入号码后会崩溃? 请帮助我,因为我是新手,不知道为什么?

input()
在Python 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();
如果用户没有输入有效的数字,
int()
调用将引发一个
ValueError
。根据您希望执行的错误处理量,您可能希望捕获该异常。有关如何工作的更多信息,请参阅


Python不需要这些
分号;您可以安全地从代码中删除它们。

请将错误添加到question@YuriMalheiros没有错误。我以exe/py文档的形式打开ir,它在我输入后关闭number@OliverMurfett:出现错误,但控制台打开的时间不够长,您无法看到它。从命令行或在空闲(Python附带的IDE)中运行程序。注意运行代码所使用的Python版本。2和3的行为完全不同。
>>> '10' < 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()
number = int(input("Enter number: "))