2013-10-13 37 views
-3

我是一個絕對的Python初學者。我編寫了一個程序來檢查數字是否爲素數。但它給了我上述類型的錯誤Python TypeError:並非在字符串格式化過程中轉換的所有參數

該錯誤的含義是什麼,我該如何解決它?

我看到相同標題的問題。但我不明白如何解決它。所以我問這個問題。

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

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 
+0

雅太感謝你了 – AnV

相關問題