eric424
eric424

Reputation: 3

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)

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


#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. 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. 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?

    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"

Upvotes: 0

Views: 41

Answers (1)

SHINJI.K
SHINJI.K

Reputation: 308

I think the problem lies in your if statement.

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().

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.

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():

Upvotes: 1

Related Questions