How can I stop my code from getting this error?

You could start w/importing numpy as np.

You’re also trying to add strings (“correct”, “incorrect”) to a number and you cannot do that. You have to convert the data type of the numpy array. (most likely)

Further, did you research the exact error message? What did it say?

https://numpy.org/doc/stable/user/basics.types.html

Here’s is a fixed and optimized code, along with explanations for each fix:

import numpy as np

rng = np.random.default_rng()

# Generate a random number between 1 and 100
np_number = rng.integers(low=1, high=101, size=1)[0]  # Convert the array to an integer

print("Guess a number between 1 and 100")

# Convert the player's input to an integer
pla_number = int(input())

if pla_number == np_number:
    answer = "correct"
else:
    answer = "incorrect"

print(f"{answer} the answer was {np_number}")

# You don't need a loop to print np_number as it's already a single integer

Explanations for the fixes:

  1. Added [0] to np_number to convert the array to an integer since rng.integers returns an array.

  2. Used an f-string for the print statement to make it more readable.

  3. Removed the unnecessary loop for printing np_number since it’s just a single integer.

Now, the code should work correctly and efficiently. It generates a random number, asks the player to guess a number, and then informs the player if their guess was correct or incorrect.