I get this error:
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
Line 37
http://pastebin.com/LhMS9Xhx
filter = [[1,1,1],[1,1,1],[1,1,1]]
activefield = [[1,2,3],[4,5,6],[7,8,9]]
newvalue = 0
newvalue+= filter[iii][jjj]*aktuellesFeld[iii][jjj]
Line 37 is
neuerGrauwert += filter[iii][jjj]*aktuellesFeld[iii][jjj]
aktuellesFeld[iii][jjj] = im.getpixel((...)) is a tuple such as:
In [8]: im.getpixel((125,125))
Out[8]: (11, 11, 11, 255)
Multiplying by a float (like filter[iii][jjj]) concatenates copies of the tuple:
In [9]: 2*im.getpixel((125,125))
Out[9]: (11, 11, 11, 255, 11, 11, 11, 255)
But neuerGrauwert is an int. And you can not add a tuple to an int.
That's very straight forward: you can't add a tuple to an int. aktuellesFeld[iii][jjj] is a tuple, which you multiply with an int (filter[iii][jjj]) resulting in a tuple. You then add that tuple to another int (neuerGrauwert), and that's a TypeError
Your code is very unreadable. Why use variable names such as iii or jjj? Also, it's a bad idea to use filter as a variable name as it is a built-in function.
You try to add a tupel to an int. Look inside your tuple, extract the value you want, and then add it to the int.
Related
I've been working on a space shooting game in which the particles attached to the ship's engine resemble the fire that comes out of the engine. I used ship_rect.midleft to give the information on the position that the particles were generated, yet the command prompt responded to me:
ship_particle [0][0] += ship_particle [1]
TypeError: can only concatenate tuple (not "int") to tuple
What should I do with this?
ship_particles = []
while True
......
ship_particles.append([[ship_rect.midleft],-3, random.randint(5,6)])
for ship_particle in ship_particles:
ship_particle [0][0] += ship_particle [1]
ship_particle [2] -= 0.1
pygame.draw.circle(screen,(255,255,255),ship_particle[0],ship_particle[2])
ship_particle_copy = [ship_particle for ship_particle in ship_particles if ship_particle[2]>0]
ship_particles = ship_particle_copy
ship_rect.midleft is a tuple. Actually you are appending a list with 1 tuple. Since you cannot change the elements of a tuple you have to make a list from the tuple. Use the asterisk (*) operator and unpacking the tuple. Just add [*ship_rect.midleft] instead of [ship_rect.midleft]
ship_particles.append([[ship_rect.midleft],-3, random.randint(5,6)])
ship_particles.append([[*ship_rect.midleft], -3, random.randint(5,6)])
Alternatively use the built-in list function:
ship_particles.append([list(ship_rect.midleft), -3, random.randint(5,6)])
Compare
ship_rect = pygame.Rect(10, 10, 20, 20)
print([ship_rect.midleft])
[(10, 20)]
and
ship_rect = pygame.Rect(10, 10, 20, 20)
print([*ship_rect.midleft])
[10, 20]
Here is what I wanted to do:
-An list V with random numbers, the quantity of numbers is given by the user
-An list P with the same quantity of numbers as of list V, but only 0's and 1's, these match the list V ( if the number is even on List V, it shows 0, if the number is not even, it shows 1)
I tried to solve this, but I got the error of TypeError: unsupported operand type(s) for %: 'type' and 'int'; how can I do this without getting it? here's my code:
import random
list_V = [random.randint (1,100) for i in range (1,dim +1)]
print(list_V)
list_P =[x for x in list_V if isinstance(x, (int))]
divider = 2
list_P =[int % divider]
print(list_P)
int refers to the int type, not a particular int that you can apply mathematical operators to, or all of the ints in a particular list, or anything like that. The syntax you want is [x ... for x in ...].
Note that the comprehension where you filter list_V to only int instances is not necessary, because random.randint can only ever return ints in the first place.
import random
dim = int(input("How many numbers? "))
V = [random.randint(1, 100) for _ in range(dim)]
P = [x % 2 for x in V]
print(V)
print(P)
How many numbers? 10
[64, 58, 24, 43, 69, 58, 16, 2, 54, 55]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 1]
I am trying to get better at writing more 'clean' and/or elegant code. I have seen examples around but I can't make them work for this particular example. Logic is very simple and I'm hoping that with some pointers on punctuation or syntax I can pick up the habit.
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
elist = []
evenlist = [i for i in a if a % 2 == 0]
print(evenlist)
I have only been able to make this work on the longer format here below:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
elist = []
for i in a:
if i % 2 == 0:
elist.append(i)
print(elist)
Should be this:
evenlist = [i for i in a if i % 2 == 0]
evenlist = [i for i in a if a % 2 == 0]
# ^
# Hmmm!
You probably wat to be checking i (an element) for evenness, rather than a (the entire list). Checking a list for evenness doesn't make much sense unless there's some magic happening in the background that ensures every element in the list is even. But there's not:
>>> [1,2,3] % 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'list' and 'int'
As an aside, there's another way to do this, specifically:
list(filter(lambda x: x % 2 == 0, a))
I think this is less readable myself but I suspect it may be more space-efficient if you just want to process them rather than create a list from them:
for item in filter(lambda x: x % 2 == 0, a):
do_something_with(item)
That doesn't create a whole new list, instead simply retrieving values from the existing list that match the filter criteria. Probably won't matter unless your lists tend to get large.
I am new to python programing.I am unable to create the following code:
Define a function even_or_odd, which takes an integer as input and returns the string even and odd, if the given number is even and odd respectively.
Categorise the numbers of list n = [10, 14, 16, 22, 9, 3 , 37] into two groups namely even and odd based on above defined function.
Hint : Use groupby method of itertools module.
Iterate over the obtained groupby object and print it's group name and list of elements associated with a group.
I have tried below code:
import itertools
def even_or_odd(n):
if n%2==0:
return 'even'
else:
return 'odd'
group=[]
uniquekeys=[]
n = [10, 14, 16, 22, 9, 3 , 37]
for k,g in itertools.groupby(n, even_or_odd(n)):
groups.append(list(g))
uniquekeys.append(k)
It is giving "TypeError: unsupported operand type(s) for %: 'list' and 'int' "
When you try to pass itertools.groupby even_or_odd you call it with n. even_or_odd takes an int, but n is a list. Just change it to itertools.groupby(n, even_or_odd)
Also you append to groups when I think you mean group.
I am currently learning python coding and I come across this qns on a learning site:
Create a function that takes a sequence of inputs (may be a list, a tuple, or just a bunch of inputs). The function should return the minimum and the maximum of the list.
This are some of the test values that are using:
minmax(5,4)
4,5
minmax(5,4,8,3,5,7,4)
3,8
minmax([5,4,6])
4,6
minmax(5.1,4.2,68.34,129.1,-90.3)
-90.3,129.1
And I had tried doing it this way but when the parameter is a list, I can't seems to convert it into a tuple and find the max and min.
Here is what I had tried:
def minmax(*a):
b = tuple(a)
minNum = min(b)
maxNum = max(b)
c = (minNum, maxNum)
return c
When a list is taken in, the return result is ([5, 4, 6], [5, 4, 6])
def minmax(*a):
if len(a) == 1: # Single (list, tuple, or scalar) argument
try:
return minmax(*a[0]) # Expansion of sequence elements, if possible
except TypeError: # Case of minmax(42)
pass # The general code below handles this directly
return (min(a), max(a))
>>> minmax(3, 5, 1, 10)
(1, 10)
>>> minmax([3, 5, 1, 10])
(1, 10)
>>> minmax((42, 123, -12))
(-12, 123)
>>> minmax(42)
42
This works in more cases than the built-in min() and max(), which do not work on a single scalar argument (min(42)).
>>> min(42)
TypeError: 'int' object is not iterable
It is however possible to write a simpler version that behaves like the built-in min() and max() (see my other answer, for instance).
This works by forcing min() to be given strictly more than 1 element, except in the special case of minmax(42), which calls min((42,)).
To be able to handle different ways of passing in arguments, you need to have some conditions to handle each case.
>>> def minmax(*a):
... if len(a) == 1 and hasattr(a[0], '__getitem__'):
... # handle a single sequence passed in
... return min(a[0]), max(a[0])
... # handle values passed in
... return min(a), max(a)
...
>>> minmax(5, 4)
(4, 5)
>>> minmax(5, 4, 8, 3, 5, 7, 4)
(3, 8)
>>> minmax([5, 4, 6])
(4, 6)
>>> minmax(5.1, 4.2, 68.34, 129.1, -90.3)
(-90.3, 129.1)
A simpler but slightly less powerful solution that mirrors my other solution is:
def minmax(*a):
if len(a) == 1: # Single (list or tuple) argument
return (min(a[0]), max(a[0]))
return minmax(a) # Single tuple argument given to minmax()
This version forces min() to be given a single (list or tuple) argument.
It behaves like the built-in min() and max(): min(42) and minmax(42) both raise an exception:
>>> minmax(3, 5, 1, 10)
(1, 10)
>>> minmax([3, 5, 1, 10])
(1, 10)
>>> minmax((42, 123, -12))
(-12, 123)
>>> minmax(42)
TypeError: 'int' object is not iterable
>>> min(42)
TypeError: 'int' object is not iterable