[英]How do I call the variable in one method in another method inside the same class?
I'm very new to Python and I use Spyder to do my Python homework.我对 Python 非常陌生,我使用 Spyder 来完成我的 Python 作业。 I am making a code that requires using class to find out a student's average marks.我正在编写一个代码,需要使用 class 来找出学生的平均分数。 Here's my code:这是我的代码:
class Student:
def __init__(self,name='default student'):
if name==None:
self.name='default student'
elif name!=None:
self.name=name
def quizcalc(self,*marks):
s=0
for x in marks:
s+=x
return s/3
def printdetail(self):
print("Hello ",self.name)
print("Your average quiz score is ",self.quizcalc) #something is not right in this line
s1 = Student()
s1.quizcalc(10)
print('--------------------------------')
s1.printdetail()
s2 = Student('Harry')
s2.quizcalc(10,8)
print('--------------------------------')
s2.printdetail()
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()
and this is what the output looked like:这就是 output 的样子:
--------------------------------
Hello default student
Your average quiz score is <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AEA48>>
--------------------------------
Hello Harry
Your average quiz score is <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AE708>>
--------------------------------
Hello Hermione
Your average quiz score is <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AE348>>
but my desired output is this:但我想要的 output 是这样的:
--------------------------------
Hello default student
Your average quiz score is 3.3333333333333335
--------------------------------
Hello Harry
Your average quiz score is 6.0
--------------------------------
Hello Hermione
Your average quiz score is 9.666666666666666
You just need to add parenthesis你只需要添加括号
self.quizcalc()
Without parenthesis you're just printing the function itself instead of the result of running that function.如果没有括号,您只是打印 function 本身,而不是运行 function 的结果。
And for fun, let me illustrate a scenario where you might actually want to print out the function name like you were doing.为了好玩,让我举例说明一个场景,您可能真的想像以前那样打印出 function 名称。
So lets say you have the class所以假设你有 class
class Student:
def quizcalc_integer(self, *marks):
pass # code goes here
def quizcalc_decimal(self, *marks)
pass # code goes here
def printdetail(self, quiz_function):
print("Hello ",self.name)
print("Your average quiz score is ",quiz_function())
s1 = Student()
And if you're working with decimal numbers you can do如果你正在使用十进制数字,你可以这样做
s1.printdetail(s1.quizcalc_integer)
And if they are not decimals如果它们不是小数
s1.printdetail(s1.quizcalc_decimal)
Very fictitious example, but if you are trying to debug what is going on and you want to know what function is stored inside of the variable quiz_function
, you would then have a reason to do print(quiz_function)
instead of print(quiz_function())
.非常虚构的示例,但是如果您尝试调试正在发生的事情并且想知道 function 存储在变量quiz_function
中,那么您将有理由执行print(quiz_function)
而不是print(quiz_function())
.
And that is my long unnecessary answer to why it's even possible to print the name of a function in the first place.这就是我对为什么首先可以打印 function 的名称的长期不必要的回答。
If you are allowed to change class strucure then you can do below changes:如果您被允许更改 class 结构,那么您可以进行以下更改:
class Student:
def __init__(self,name='default student'):
self.average=0.0 #1st change
if name==None:
self.name='default student'
elif name!=None:
self.name=name
def quizcalc(self,*marks):
s=0
for x in marks:
s+=x
self.average=s/3 #2nd change
def printdetail(self):
print("Hello ",self.name)
print("Your average quiz score is ",self.average)#3rd change
s1 = Student()
s1.quizcalc(10)
print('--------------------------------')
s1.printdetail()
s2 = Student('Harry')
s2.quizcalc(10,8)
print('--------------------------------')
s2.printdetail()
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.