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

Python多边形类实例化报错TypeError:缺失参数x的原因与解决

Fixing the TypeError and Refactoring Your Polygon Class

Let's walk through why you're getting that error and fix up your code to work as intended.

1. Why the TypeError Happens

The error message TypeError: __init__() missing 1 required positional argument: 'x' spells out exactly what's wrong:

  • Your polygon class's constructor method (__init__) is defined to require one parameter x, but when you create an instance with stone = polygon(), you aren't passing any arguments to it.
  • Python can't locate the required x value it needs to initialize the class, so it throws this error.

On top of that, your current code has a few other logic inconsistencies we'll address along the way.

2. Key Issues in the Original Code

Let's list out the other problems to fix:

  • The __init__ method has conflicting code: you first assign self.x = x, then immediately overwrite it with self.x = []—this makes the initial x parameter completely useless.
  • The __inputSides__ method re-prompts for the number of sides, which defeats the purpose of passing it to the class initially.
  • You call stone.__dispSides__(4) with a number, but the method expects a list of side lengths—this would cause a second error once you fix the first one.
  • Using double underscores (__method__) for your custom methods is unnecessary here. That naming convention is reserved for Python's built-in special methods (like __init__ or __str__). For your own methods, use single underscores or plain names.

3. Refactored Working Code

Here's the fixed version with explanations of each change:

class Polygon:
    # Follow PEP8 convention: use PascalCase for class names
    def __init__(self, num_sides):
        # Store the number of sides as an instance attribute
        self.num_sides = num_sides
        # Initialize an empty list to hold side lengths
        self.side_lengths = []

    def input_sides(self):
        # Use the instance's stored num_sides instead of re-prompting
        print(f"Enter {self.num_sides} side lengths:")
        for i in range(self.num_sides):
            length = float(input(f"Side {i+1} length: "))
            self.side_lengths.append(length)

    def display_sides(self):
        # Pull side lengths directly from the instance's attribute
        print("Polygon side lengths:")
        for idx, length in enumerate(self.side_lengths, 1):
            print(f"Side {idx}: {length}")

# Step 1: Get number of sides from the user
num_sides = int(input("Enter number of sides: "))
# Step 2: Create Polygon instance with the required num_sides argument
stone = Polygon(num_sides)
# Step 3: Collect side lengths
stone.input_sides()
# Step 4: Display the collected lengths
stone.display_sides()

What Changed:

  • Renamed the class to Polygon (aligning with Python's style guidelines).
  • The __init__ method now properly stores the number of sides and initializes an empty list for side lengths.
  • input_sides uses the instance's num_sides value instead of asking again, and saves lengths directly to the instance's side_lengths attribute.
  • display_sides reads from the instance's stored lengths, so you don't need to pass a list to it.
  • We now create the instance correctly by passing num_sides to Polygon(), matching the __init__ method's requirements.

4. Example Run

Enter number of sides: 4
Enter 4 side lengths:
Side 1 length: 5
Side 2 length: 5
Side 3 length: 5
Side 4 length: 5
Polygon side lengths:
Side 1: 5.0
Side 2: 5.0
Side 3: 5.0
Side 4: 5.0

内容的提问来源于stack exchange,提问作者John Rawls

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:11:22