Python Forum
unsupported operandtype(s) for -: 'list' and 'int' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: unsupported operandtype(s) for -: 'list' and 'int' (/thread-26296.html)



unsupported operandtype(s) for -: 'list' and 'int' - Lakkad - Apr-27-2020

Here is the basic code.

for a in range(1, 5):
for b in range(0+[a*2]-2],2*a)
print(b)

It shows error unsupported operandtype(s) for -: 'list' and 'int'

I have tried everything, it seems that in the range of second for loop 'a' which is 'int'type becomes 'list'

Please help


RE: unsupported operandtype(s) for -: 'list' and 'int' - anbu23 - Apr-27-2020

Use paranthesis in calculation. [] operator is used for list

for a in range(1, 5):
 for b in range(0+(a*2)-2,2*a):
  print(b)



RE: unsupported operandtype(s) for -: 'list' and 'int' - pyzyx3qwerty - Apr-27-2020

Please use proper code tags while posting your thread. It will be much clearer for us to understand.
So, when you run the code, you have to understand what do you mean by for a in range(1,5):. That basically means all the integers from 1 to 5, excluding 5 itself. Now, you have added another statementfor b in range([0+[a*2]-2],2*a) :. First of all, you should understand that when you type [0+[a*2]-2], python takes that as a list and therefore, the error:
Error:
TypeError: unsupported operand type(s) for +: 'int' and 'list'
So, use the parentheses(), instead of square brackets. The square brackets[] is specifically used in lists
Hope this helps!