Python类内函数返回值传递问题及MIT资格校验代码调试
Fixing MIT Eligibility Check in Your Python Class
Great question—let's break down what's going wrong with your code and get that eligibility check working correctly.
Key Issues in Your Current Code
There are two critical problems causing the incorrect "ineligible" result even with perfect scores:
- You're not calling the checker methods (and using the wrong context)
In youreligiblemethod, you're referencingmitstudent.gpachecker(the method itself, not its return value) instead of calling the instance's method withself.gpachecker(). Using the class name here doesn't access the current instance's data (like the GPA the user entered). - Broken conditional logic
Your current conditionmitstudent.gpachecker and mitstudent.actchecker and mitstudent.satchecker == "eligible"only checks if the last value equals "eligible". The first two parts are just method objects, which evaluate toTruein a boolean context—so even if GPA/ACT are too low, the condition would still pass if SAT is eligible (or fail incorrectly if SAT isn't, regardless of other scores).
Corrected mitstudent.py Code
Here's the fixed version of your class, with explanations of changes:
#8/28/2020 #Inheritance class mitstudent: def __init__(self): 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?: ")) def gpachecker(self): if self.gpa >= 3.5: # No need to cast to float again—self.gpa is already a float return "eligible" else: return "not eligible" def actchecker(self): if self.act >= 33: return "eligible" else: return "not eligible" def satchecker(self): if self.sat >= 1400: return "eligible" else: return "not eligible" def eligible(self): # Call each checker method on the current instance (self) and check all return values gpa_ok = self.gpachecker() == "eligible" act_ok = self.actchecker() == "eligible" sat_ok = self.satchecker() == "eligible" if gpa_ok and act_ok and sat_ok: return "This student is eligible for MIT" else: return "This student is ineligible for MIT"
Fixes to the Main File
Your main script also has a small issue with how you're calling the eligible method. Here's the corrected version:
#Inheritance #8/28/2020 from mitstudent import mitstudent from harvardstudent import harvardstudent name = input("What is your name?: ") # No need for str()—input() already returns a string # Create an instance of mitstudent (this will trigger the input prompts for scores) try: student = mitstudent() except ValueError: print("Please enter valid numerical values for scores.") else: # Call eligible() on the instance, not the class print(student.eligible())
Why This Works
- Using
self: When you callself.gpachecker(), you're running the method on the specific instance ofmitstudentyou created—so it uses the GPA/ACT/SAT values that user entered for that student. - Clear conditional checks: We first capture whether each requirement is met, then check if all three are true. This makes the logic easy to read and debug.
- Proper method invocation: Calling
student.eligible()instead ofmitstudent.eligible(name)uses the instance's data directly, which is how OOP is supposed to work in Python.
Now if you enter perfect scores (like 4.0 GPA, 36 ACT, 1600 SAT), the code will correctly return that the student is eligible for MIT.
内容的提问来源于stack exchange,提问作者eric424
相关产品推荐
相关产品推荐

