【问题标题】:Python TypeError: not all arguments converted during string formattingPython TypeError:字符串格式化期间并非所有参数都转换
【发布时间】:2026-01-10 22:35:01
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7


    【解决方案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
    

    【讨论】: