Heres what i wanted to do:
-An list V with random numbers, the quantity of numbers is given by the user
-An list P with 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 number is not even, it shows 1)
Tried to solve this,but i get the error of TypeError: unsupported operand type(s) for %: ‘type’ and ‘int’, how can I do this without getting it? heres 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)
>Solution :
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]