def generateHand():
cardOne = ("one", "two", 'three', "four", "five", "six", 'seven', "eight", "nine", "10",
"jack", "queen", "king")
suits = ["spades", "diamonds", "hearts", "clubs"]
suitUse = np.random.choice(suits, 1)
cardOneVar = np.random.choice(cardOne, 1)
print ("A" cardOneVar "Of" suitUse)
This code is generating this error.
File "main.py", line 25, in generateHand print ("A" cardOneVar "Of" suitUse) numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U5')) -> None
Does anybody know what the issue is? The error began after I changed the code to print writing and cardOneVar
rather than just the suit variable. The indent errors are because of the issues with copy pasting, they are not in the code.
CodePudding user response:
It seems like the return type of np.random.choice(), which is an np.ndarray, is incompatible with concatenation. Try using:
suitUse = np.random.choice(suits, 1)[0]
cardOneVar = np.random.choice(cardOne, 1)[0]
this will make the types of suitUse and cardOneVar strings instead, by choosing the first (and only) value in the ndarray.
CodePudding user response:
In [119]: suits = ["spades", "diamonds", "hearts", "clubs"]
The result of your choice
is a 1d, 1 element, array with string dtype:
In [120]: x = np.random.choice(suits,1)
In [121]: x
Out[121]: array(['spades'], dtype='<U8')
It can be formatted with:
In [122]: print(f'Of {x}')
Of ['spades']
or without the brackets:
In [123]: print(f'Of {x[0]}')
Of spades
If we omit the size value, the result is string "scalar", that does 'add':
In [124]: x = np.random.choice(suits)
In [125]: x
Out[125]: 'spades'
In [126]: 'Of ' x
Out[126]: 'Of spades'
In [127]: print(f'Of {x}')
Of spades
In [128]: type(x)
Out[128]: numpy.str_