如何将类中函数的返回值传递给同一类中的另一个函数



我正在与类对象合作,并在类对象方面做得越来越好,我开始了一个新项目,该项目将根据用户的GPA、SAT和ACT成绩来检查用户是否有资格进入麻省理工学院或哈佛大学(不要核实我,我认为这只是一个有趣的项目,我脑海中浮现出了一些数字(

我还没有开始参与我的哈佛资格项目,所以我只会使用麻省理工学院的部分。

这是我的主文件


#Inheritance
#8/28/2020
from mitstudent import mitstudent #This is importing both of the classes
from harvardstudent import harvardstudent
name = str(input("What is your name?: ")) #Asking the user's name to use as an argument for the parameter
while True: #This while loop using try and except to make sure that the user inputs a number instead of a string
try:
name = mitstudent()
except ValueError:
print("Input a number")
else:
break
print(mitstudent.eligible(name))

这是我的mitstudent.py文件,其中包含我的类


#8/28/2020
#Inheritance
class mitstudent:
def __init__(self): #These are my class objects, the student will input their GPA, ACT score, and SAT score and the
#function will input it as the objects
self.gpa = float(input("What is your gpa?: "))
self.act = float(input("What is your ACT score?: "))
self.sat = float(input("What is your SAT score?: "))

'''
The next three class functions will be to check if the gpa, act, or sat scores are "eligible" for MIT if they are, the 
function will return a value of "eligible" and if they aren't the function will return a value of "not eligible"
'''
def gpachecker(self):
if float(self.gpa) >= 3.5:
return "eligible"
else:
return "not eligible"
def actchecker(self):
if float(self.act) >= 33:
return "eligible"
else:
return "not eligible"
def satchecker(self):
if float(self.sat) >= 1400:
return "eligible"
else:
return "not eligible"
def eligible(self): #This function checks to see if the student has met all of the requirements to be eligible for
#Mit, which includes a gpa over 3.5, an act score over 33, and an sat score over 1400
if mitstudent.gpachecker and mitstudent.actchecker and mitstudent.satchecker == "eligible":
return "This student is eligible for MIT"
else:
return "This student is ineligible for MIT"

在主文件中,我为所有对象设置了一个名称并输入了9999,但它仍然表示该学生不合格。我相信这是因为gpachecker(act&sat(函数中的return语句实际上并没有按照我想要的方式返回

def gpachecker(self):
if float(self.gpa) >= 3.5:
return "eligible"
else:
return "not eligible"

是否要在if语句中实际使用它?

def eligible(self): #This function checks to see if the student has met all of the requirements to be eligible for
#Mit, which includes a gpa over 3.5, an act score over 33, and an sat score over 1400
if mitstudent.gpachecker and mitstudent.actchecker and mitstudent.satchecker == "eligible":
return "This student is eligible for MIT"
else:
return "This student is ineligible for MIT"

我认为问题出在if语句中。

if mitstudent.gpachecker and mitstudent.actchecker and mitstudent.satchecker == "eligible":

条件评估如下:

mitstudent.gpachecker and mitstudent.actchecker and (mitstudent.satchecker == "eligible")

首先,如果要获取方法返回的值,必须使用self.method_name()调用它。

虽然mitstudent.gpacheckermitstudent.actcheckermitstudent.satchecker的值将始终是True,因为它们属于类的方法,但鉴于mitstudent.satchecker是一个函数,而不是字符串,(mitstudent.satchecker == "eligible")始终是False

一个解决方案是:

if self.gpachecker() == "eligible" and self.actchecker() == "eligible" and self.satchecker() == "eligible":

您可能还想修改检查器方法,以返回布尔值(True或False(而不是字符串,这样您的条件就会变短:

if self.gpachecker() and self.actchecker() and self.satchecker():

最新更新