You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中School类mouse方法参数缺失报错及多返回值需求问题

Fixing TypeError & Implementing Your School Class Features

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 logical and instead of just printing both values. If you want to print them for debugging, replace it with print(num_of_class, num_of_divisions).
  • Using self.num_of_class and self.num_of_divisions saves these values to the School object, 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 file1 to import fil2 (this was a typo—your module file is named fil2.py!)
  • Renamed variables in the loop for clarity (no more reusing divisionloop for two different purposes)
  • Created a School instance (my_school = fil2.School()) before calling mouse—this is the key fix for the TypeError.
  • Added optional print statements so you can confirm the values are actually saved in the School object.

How It Works Now

When you run file1.py:

  • You'll create a School object called my_school.
  • The mouse method will save num_of_class and num_of_divisions as attributes of my_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_class and my_school.num_of_divisions.

内容的提问来源于stack exchange,提问作者Uday Singh Khalsa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:44:39