So what I'm trying to do is create a function that'll throw 2 dice n times. For every throw, if at least one dice number is odd, we'll return the sum of the pair's values, if not we just return None. The objective of the program is to return a matrix that has 3 columns and n amount of rows representing each pair of dice thrown. Column 1 would be the value of the first dice, column 2 would be the value of the second dice, and column 3 would either be the sum of 1 & 2 or None.
This is the code I have thus far:
def dice(n):
rolls = np.empty(shape=(n,3))
for i in range(n):
x = random.randint(1,6)
y = random.randint(1,6)
if x or y == 1 or 3 or 5:
sum_2_dice = x + y
rolls.append(x,y,sum_2_dice)
if x and y != 1 or 3 or 5:
sum_2_dice = None
rolls.append(x,y,sum_2_dice)
return rolls
Running the function gives me no problems, however when I test it out like dice(2)
I get AttributeError: 'numpy.ndarray' object has no attribute 'append'
I assumed we'd have to start off with an empty numpy matrix, and then be able append it in the for loop to get the the 3 columns (the for loop would repeat n times, and we'd consequently get a matrix with n rows and 3 columns)
We're only allowed to inport numpy and random, btw.