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

新手遇List Index out of Range错误:数独方阵校验问题求助

Hey there! Let's work through fixing your check_square function—you're super close, just got a couple of logic missteps here.

What's Causing the IndexError

The main issue is your while square[i][j] <= len(square): condition. This line is comparing the value of the element in the square to the length of the square, not checking whether your index is valid.

When you pass a non-square matrix like [[1,2,3],[2,4,1],[3,1,4],[4,3,2]], you have 4 rows but the first 3 rows only have 3 elements each. When j increments to 3, you're trying to access square[0][3]—but that row only goes up to index 2, hence the IndexError.

On top of that, your current logic only checks the first row's length and assumes all rows are the same, which isn't safe. A valid n×n square requires every row to have exactly n elements, where n is the total number of rows.

This version is simpler, easier to read, and avoids index errors entirely by leveraging Python's built-in len() function and for loops:

def check_square(square):
    # Handle empty square edge case
    if not square:
        print("False")
        return False
    
    total_rows = len(square)
    # Check every row to ensure its length matches the total number of rows
    for row in square:
        if len(row) != total_rows:
            print("False")
            return False
    
    print("True")
    return True

How This Works:

  1. First, we check if the square is empty to avoid edge cases where len(square) is 0.
  2. We get the total number of rows in the square.
  3. We loop through each row, verifying that each row's length is equal to the total number of rows. If any row fails this check, we immediately return False.
  4. If all rows pass, we return True.

Solution 2: Fixing Your Original While Loop Logic

If you want to stick with while loops to practice that pattern, here's how to adjust your code to avoid index errors and fix the logic:

def check_square(square):
    if not square:
        print("False")
        return False
    
    total_rows = len(square)
    countRow = 0
    i, j = 0, 0

    # Fix: Check if j is within the bounds of the current row, not comparing element values
    while j < len(square[i]):
        countRow += 1
        j += 1
    
    # Now verify ALL rows have the same length as total_rows (not just the first)
    for row in square:
        if len(row) != total_rows:
            print("False")
            return False
    
    print("True")
    return True

Key Fixes Here:

  • Changed the while condition to j < len(square[i])—this checks if j is a valid index for the current row, so we never try to access an element that doesn't exist.
  • Added a loop to check every row's length, not just assume the first row represents all rows.

Testing the Fixes

  • For your valid 4×4 square [[1,2,3,4],[2,4,1,3],[3,1,4,2],[4,3,2,1]], both solutions will return True.
  • For your invalid 4-row, 3-column square, both solutions will return False without triggering an IndexError.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:46:47