I am getting error "not all arguments converted during string formatting" while running this code please explain how to fix this and what is this error
def multiples(l,n):
count=0
for i in l:
if(i%n==0):
count+=1
return count
m=input("no of elements: ")
l=[]
print("enter elements: ")
for i in range(int(m)):
l.append(input())
n=int(input("enter no to find no.of multiples: "))
print(multiples(l,n))
error:
Exception has occurred: TypeError
not all arguments converted during string formatting
line 4, in multiples
if(i%n==0):
line 13, in <module>
print(multiples(l,n))
TypeError: not all arguments converted during string formatting
The problem is that the list l
that you are passing to multiples
has strings inside instead of int which causes the code to call %
on a string instead of modulo. You don't get a straightforward error as %
acts a format string like "% is my name" % name
. To fix it you can convert the input()
to int as so: l.append(int(input()))
so your overall code would be
def multiples(l,n):
count=0
for i in l:
if(i%n==0):
count+=1
return count
m=input("no of elements: ")
l=[]
print("enter elements: ")
for i in range(int(m)):
l.append(int(input()))
n=int(input("enter no to find no.of multiples: "))
print(multiples(l,n))