简体   繁体   English

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

[英]How do I pass a return value in a function in a class to another function within the same class?

I'm working with and getting better at class objects and I started a new project that will check to see if the user is eligible for MIT or Harvard based on their GPA, SAT, and ACT scores (don't fact check me I thought this would just be a fun project and came up with the numbers off the top of my head)我正在与班级对象合作并变得越来越好,我开始了一个新项目,该项目将根据用户的 GPA、SAT 和 ACT 分数检查用户是否有资格进入麻省理工学院或哈佛这只是一个有趣的项目,并且想出了我脑海中的数字)

I haven't started working on my Harvard Eligibility part of the project yet, so I'm only going to be using the MIT side.我还没有开始研究这个项目的哈佛资格部分,所以我只会使用麻省理工学院的一面。

This is my main file这是我的主文件


#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))

This is my mitstudent.py file that contains my class这是我的 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"

In the main file, I set a name and inputted 9999 for all of the objects, however it still says the student is ineligible.在主文件中,我为所有对象设置了一个名称并输入了 9999,但它仍然说该学生不合格。 I believe it is because the return statement within the gpachecker (act & sat aswell) function is not actually returning the way I want it to.我相信这是因为 gpachecker (act & sat aswell) 函数中的 return 语句实际上并没有按照我想要的方式返回。 Is there a way I can return the statement from those functions有没有办法可以从这些函数中返回语句

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

for it to actually be used in this if statement?让它在这个 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"

I think the problem lies in your if statement.我认为问题在于您的 if 语句。

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

The condition will be evaluated like this:条件将被评估如下:

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

First of all, if you want to get the value returned by the methods, you have to call it using self.method_name() .首先,如果要获取方法返回的值,必须使用self.method_name()调用它。

While the values for mitstudent.gpachecker , mitstudent.actchecker , and mitstudent.satchecker will always be True since they pertain to the methods of the class, (mitstudent.satchecker == "eligible") is always False given that mitstudent.satchecker is a function, not a string.虽然mitstudent.gpacheckermitstudent.actcheckermitstudent.satchecker的值将始终为True因为它们与类的方法有关, (mitstudent.satchecker == "eligible")始终为False因为 mitstudent.satchecker 是一个函数,而不是字符串。

A solution would be something like:一个解决方案是这样的:

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

You may also want to modify your checker methods to return Boolean(True or False) values instead of strings so that your condition would become shorter:您可能还想修改检查器方法以返回布尔值(真或假)而不是字符串,以便您的条件变得更短:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量从函数传递到同一个类中的另一个变量 - how to pass a variable from a function to another within the same class 如何在类的函数中一个接一个地传递字典的所有键值对? - How do I pass all key value pairs of a dictionary one after another in function of a class? 如何返回在类内部的函数中更改的变量,并在类外部的函数中使用它 - How Do I return a variable that is changed within a function inside a class and use it in a function outside of the class 如何模拟类的函数的返回值? - How do I mock a class's function's return value? 如何获得在另一个函数中调用的函数以返回值? 蟒蛇 - How do I get a function that is called within another function to return a value? PYTHON 如何在相同 class 的另一个 function 中使用 function 的值 - How to use value of a function in another function of same class 如何在类中传递方法的返回值? - How to pass return value of method within class? 将值从一个类/函数传递到另一个类/函数 - pass value from one class/function to another class/function 如何在 function 内的循环内返回值? - How do I return a value within a loop within a function? 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM