Python中School类mouse方法参数缺失报错及多返回值需求问题
Hey there! Let's tackle that error you're seeing and adjust the code to meet both of your requirements: storing num_of_divisions and num_of_classes in a School object, and making the mouse method return both values.
First, here's the error you're getting for reference:
Traceback (most recent call last):
File "ttmain.py", line 24, in
pak=classes.School.mouse(num_of_class, num_of_divisions)
TypeError: mouse() missing 1 required positional argument: 'num_of_divisions'
Why This Error Happens
The mouse method is an instance method—it's designed to be called on an actual object created from the School class, not directly on the class itself. When you use School.mouse(), Python treats your num_of_class value as the self parameter (which refers to the instance of the class), so it's still waiting for the num_of_divisions argument you intended to pass. That's why you get the "missing argument" error.
Step-by-Step Fixes
Let's update both your files to get everything working correctly.
1. Update the Module File (fil2.py)
We'll adjust the School class to properly store the values and return both of them:
class School: def mouse(self, num_of_class, num_of_divisions): # Store the values as attributes of the School instance self.num_of_class = num_of_class self.num_of_divisions = num_of_divisions # Return both values as a tuple (Python's way of returning multiple values) return num_of_class, num_of_divisions
- Removed the confusing
print(num_of_class and num_of_divisions)line—this was using logicalandinstead of just printing both values. If you want to print them for debugging, replace it withprint(num_of_class, num_of_divisions). - Using
self.num_of_classandself.num_of_divisionssaves these values to theSchoolobject, so you can access them later if needed. - The return statement now sends back both values as a tuple, which will print as something like
(3, [2, 2, 1])depending on your input.
2. Fix the Main File (file1.py)
We need to correct the import, create a School instance first, and clean up small issues:
# Fix the import to match your module's filename (fil2.py) import fil2 name_of_school = input("Enter name of School\n") print(f"Your School's name is {name_of_school}") try: num_of_class = int(input("How many classes are there in your School?\n")) except (ValueError, TypeError): print("Please Enter a valid number") else: if num_of_class <= 0: print("Number cannot be zero or less") else: print(f"Number of classes in school are {num_of_class}") num_of_divisions = [] for divisionloop in range(num_of_class): class_num = divisionloop + 1 division_count = int(input(f"Enter number of Divisions for class {class_num}: ")) num_of_divisions.append(division_count) # First, create an instance of the School class my_school = fil2.School() # Call the mouse method on the instance, passing your two values pak = my_school.mouse(num_of_class, num_of_divisions) print(pak) # Optional: Verify the values are stored in the object print("Stored class count:", my_school.num_of_class) print("Stored divisions list:", my_school.num_of_divisions)
- Fixed the import from
import file1toimport fil2(this was a typo—your module file is namedfil2.py!) - Renamed variables in the loop for clarity (no more reusing
divisionloopfor two different purposes) - Created a
Schoolinstance (my_school = fil2.School()) before callingmouse—this is the key fix for the TypeError. - Added optional print statements so you can confirm the values are actually saved in the
Schoolobject.
How It Works Now
When you run file1.py:
- You'll create a
Schoolobject calledmy_school. - The
mousemethod will savenum_of_classandnum_of_divisionsas attributes ofmy_school. - The method returns both values as a tuple, which gets printed to the console.
- You can access the stored values anytime later using
my_school.num_of_classandmy_school.num_of_divisions.
内容的提问来源于stack exchange,提问作者Uday Singh Khalsa

