Ibrahim Ibrahim - 7 months ago 23
Python Question

Why does this result in a can't assign to operator error?

Why "can't assign to operator" error for this line

point * hours = QP

class Student(object):
def __init__(self, name, surname, grade, hours, QP):
self.name = name
self.surname = surname
self.grade = grade
self.hours = hours
self.QP = QP
def getName(self):
return '{}'.format(self.name)

def getSurname(self):
return '{}'.format(self.surname)

def getGrade(self):
return list(zip(self.grade, self.hours))

def getHours(self):
return '{}'.format(self.hours)
def point(self):
if grade == A:
point = 4.0
elif grade == B:
point = 3.0
elif grade == C:
point = 2.0
elif grade == D:
point = 1.0
point = 0.0

def getQPoints(self):
point * hours = QP
return QP

stud1 = Student("John","Brown",["A","B","A"],["15.0","25.0","20.0"],"")
stud2 = Student("Mary","Watson",["C","A","B"],["15.0","25.0","20.0"],"")

print (stud1.getQPoints())

Answer Source

I don't believe that you can reverse that. Instead, do

QP = point * hours

That way, you instantiate the new variable, and set it to the product of the points and the hours.

Some other issues: your point variable isn't defined either. You need

self.point = point

in order to have the variable as an actual variable. Also, it might cause some confusion between the point function and the point variable, because of the two being the same name. One or the other, I'd name one as points.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download