-3

我是python的绝对初学者。我编写了一个程序来检查一个数字是否是素数。但它给了我上述类型错误

该错误的含义是什么,我应该如何解决它?

我看到了同名的问题。但我不明白如何解决它。所以我在问这个问题。

num = ( "which no. u want to check prime or not:" )
i = 1
k = 0
while(i <= num):
  if(num % i == 0): #idle is showing type error here 
       k=k+1
       i=i+1
  if(k == 2):
       print "%d is prime number" % num
  else:
       print "%d is not a prime no" % num
4

1 回答 1

1

num是字符串。

>>> num = ( "which no. u want to check prime or not:" )
>>> num % 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>

我想你错过了raw_input()

>>> num = int(raw_input( "which no. u want to check prime or not:" ))
which no. u want to check prime or not:1
>>> num
1
于 2013-10-13T11:32:35.507 回答