I’ve had a look at other questions on stackoverflow but they are different and don’t seem to apply to this question, so here goes.
I’ve written a simple script that gives me a print out of every number between 1-49 and puts it into a list using the range function.
Now I have defined a function to work out what numbers are odd and what numbers are even, here is my code:
def check(number): if number%2==0: print "Even Numbers:",(number) else: print "Odd Numbers:",(number) a = range(1,50) print a check(a)
I get the following error when I run the script:
unsupported operand type(s) for %: 'list' and 'int'
So I know that this means the % operator cannot doesn’t support ‘lists’ or ‘ints’, but how can I fix it?
I tried this:
def check(number): if number%2==0: print "Even Numbers:",(number) else: print "Odd Numbers:",(number) a = range(1,50) b = str(a) check(str(a))
But get the error:
Traceback (most recent call last): File "showEvenNumbers.py", line 12, in <module> check(str(a)) File "showEvenNumbers.py", line 2, in check if number%2==0: TypeError: not all arguments converted during string formatting
So I’m a bit unsure what to do.
Any help would be much appreciated.
a is a list, but
check expects a single integer. You need to iterate over the list:
for item in a: check(item)
Answered By – Blender
Answer Checked By – Pedro (AngularFixing Volunteer)