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
polygonclass's constructor method (__init__) is defined to require one parameterx, but when you create an instance withstone = polygon(), you aren't passing any arguments to it. - Python can't locate the required
xvalue 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 assignself.x = x, then immediately overwrite it withself.x = []—this makes the initialxparameter 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_sidesuses the instance'snum_sidesvalue instead of asking again, and saves lengths directly to the instance'sside_lengthsattribute.display_sidesreads 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_sidestoPolygon(), 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
相关产品推荐
相关产品推荐

