[英]I am confused why is the return function printing a value on the console in my program. Isn't it just supposed to return a value?
My program overloads the + operator to calculate the average GPA of 2 students. 我的程序使+运算符超载,以计算2个学生的平均GPA。 In my definition of the add function, I expect the return statement to return the average value.
在我对add函数的定义中,我期望return语句返回平均值。 I then want to print this 'returned value' using print().
然后,我想使用print()打印此“返回值”。
However, the return statement also seems to print the average value along with the print statement. 但是,return语句似乎也将平均值与print语句一起打印。
The output was: 输出为:
8.0
8.0
I don't understand why the return statement prints the value. 我不明白为什么return语句会显示该值。 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). 通过在网上搜索这个问题,我发现只有在使用交互式控制台时(我正在使用pycharm),return才应该打印一个值。
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...
检查那里是否有打印件...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.