'numpy.str_' object cannot be interpreted as an integer in Python

Dung Do Tien May 03 2022 165

Hi Guys, I have two arrays that contain names and scores of subjects and I want to print out of them one by one, like this:

 import numpy as np

subjects = np.array(["Math", "physics", "literature", "Chemistry", "history"])
subject_point = np.array([5.5, 8, 9.5, 7, 6])

#  print the range of values using for loop
for i in range(len(subjects)):
    print(range(subjects[i]))

But when I was running the code above and got an exception TypeError: 'numpy.str_' object cannot be interpreted as an integer.

 Traceback (most recent call last):
  File "main.py", line 8, in <module>
    print(range(subjects[i]))
TypeError: 'numpy.str_' object cannot be interpreted as an integer

It really worked for me. Anyone can help me explain it?

I'm using Python 3.9 and Windows 11.

Thanks in Advance.

Have 1 answer(s) found.
  • s

    suman kumari May 03 2022

    You need to know the range() function only accepts an integer parameter. Your array contains many string items. So why did you use this method?

    Solution: remove range() function inside print() method. see below:

     import numpy as np
    
    subjects = np.array(["Math", "physics", "literature", "Chemistry", "history"])
    
    #  print the range of values using for loop
    for i in range(len(subjects)):
        print(subjects[i])

    #Output

     Math
    physics
    literature
    Chemistry
    history 
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close