Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

Python TypeError: not all arguments converted during string formatting

I am an absolute beginner of python. I had coded a program for checking whether a number is prime or not. but it is giving me the above type error

What is the meaning of that error and how should I resolve it?

I saw the questions with same title. but I didn't understand how to resolve it. so I am asking this question.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

num is string.

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

I think you missed 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...