Python TypeError:在字符串格式化期间不是所有参数都被转换

时间:2013-10-13 11:30:50

标签: python python-2.7

我是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

1 个答案:

答案 0 :(得分: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