My program overloads the + operator to calculate the average GPA of 2 students. In my definition of the add function, I expect the return statement to return the average value. I then want to print this 'returned value' using print().
However, the return statement also seems to print the average value along with the print statement.
The output was:
8.0
8.0
I don't understand why the return statement prints the value. Isn't is just supposed to return the value?
From googling this issue online, I found the only time when return should print a value is when an interactive console is being used (I am using pycharm).
class student():
def __init__(self, name, gpa):
self.name = name
self.gpa = gpa
def __add__(x, y):
return ((x.gpa + y.gpa)/2)
s1 = student("john", 9)
s2 = student("wick", 7)
avg = s1 + s2
print(s1 + s2)
It looks like in the first screenshot there is more source code when you scroll down. Check if a print exists there...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.