Home > Back-end >  TypeError: unsupported operand type(s) for *: '_Printer' and 'float'
TypeError: unsupported operand type(s) for *: '_Printer' and 'float'

Time:04-02

I'm trying to use numpy math in this code but I had the bug: TypeError: unsupported operand type(s) for *: '_Printer' and 'float'. Could anyone help me with this one? I don't understand the '_Printer' part. I'm new to python. Thank you!

stumarks = []
class marks_object:
  def __init__(self,CID, SID, m,crd):
    self.CID = CID
    self.SID = SID
    self.m = m
    self.crd = crd

def MarkInput(): 
 CourseID = input("Enter the course's ID : ")
 if CourseID not in [CourseInfo.id for CourseInfo in courselist]:
       print(" The course's id isn't founded! Please try again!")
 else:
  nm = int(input("Number of student that you want to enter marks: "))
  for i in range(nm):
    while True: 
      StuID = input("Enter a student's ID : ")
      if StuID not in [StudentInfo.id for StudentInfo in studentlist]:
        print("The student's ID isn't founded! Please try again! ")
        continue
      break
    marks = RoundDown()
    obj = marks_object(CourseID,StuID,marks,credits)
    stumarks.append(obj)
def avgGPA():
  sid = input("Enter the student's ID : ") 
  coursecredit = []
  coursemark = []
  avgcourse= []
  totalcrd = []
  for course in stumarks:
    if sid in course.SID:
      x = course.crd
      y = course.m
      coursecredit.append(x)
      coursemark.append(y)
      coursecredit = np.array(coursecredit)
      coursemark = np.array(coursemark)
      output = np.multiply(coursecredit,coursemark) # I HAD THE BUG IN THIS LINE

CodePudding user response:

Your error is when you create the instance obj of your mark_object in MarkInput(). You don't actually define the variable credits, and confusion comes from the fact that credits is actually a built-in variable.

If you had another variable name that didn't correspond to a built-in variable, you would have a NameError that would be raised, and it might be a lot more obvious where the problem is. However, you have now created an object with the built-in credits variable, which then throws up the error later on when you try to multiply the two arrays together. Just make sure you define your variable credits and you should be good.

  • Related