I have the following code that seeks to change the number 4 on the matrix (if 4 is rolled) to an 'x'. This can be done with Python lists, but numpy arrays require the same data type. Is there a workaround, that allows me to achieve the same result?
Code
import numpy as np
matrix=np.array([[9,10,11,12],[8,7,6,5],[1,2,3,4]])
print(matrix)
def round1():
rolldice=int(input("Roll Dice:"))
print("You have rolled:",rolldice)
if rolldice<=4:
matrix[2,rolldice-1]="X"
print(matrix)
else:
print("Greater than 3")
round1()
Line that doesn't work:
if rolldice<=4:
matrix[2,rolldice-1]="X"
What would work (this would change it to a zero)
if rolldice<=4:
matrix[2,rolldice-1]=0
Any ideas on how this could be done? I simply, based on the number rolled by the dice, want to replace the number in the matrix with an 'x'. So if 3 was rolled, 3 would be replaced by an 'x'.
As an aside, I'd also be interested in a more efficient way of replacing the relevant number in the matrix with an x based on the throw, without the use of IF functions. At the moment, I have to specify that if the throw is less than 4 (which is the length of the third list in the array), do such and such, else, I would have to go on to code another alternative for if the throw exceeded 4.
objectdtype, although that may complicate other things (e.g. arithmetic operations, etc.). Can't you use some "special" value to represent the X instead (like 0, like you said, or -1 or something)? Why does it need to be a string?matrix=np.array([[9,10,11,12],[8,7,6,5],[1,2,3,4]], dtype=object)will do what you want but I do not think it is a good idea. TheXseems to be a presentation detail, not a data item.