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

使用Sublime Text编写Python代码后代码行混乱,求原因

修复混乱的Python行列式计算程序

问题背景

我在Sublime Text中编写了一个未完成的Python程序,用于计算向量分解相关的行列式,两天后返回查看时,发现代码行缩进、逻辑完全混乱,原混乱代码如下:

matrix.append(int(a[count_a])) 
                matrix.append(int(vectors[j][i]))
                print(a[count_a], end="")
                print(vectors[j][i], end="")
            count_a2 += 1
            count_first = False
            else:
            if count_a2 == count_tringle:
            out_A.append("("+i+")")
            out_A.append(i)
            out_Ael.append("("+i+")")
            out_Ael.append(i)
            print("+"+vectors[j][i]+"λ"+str(count_alfa), end="")
            print(vectors[j][i]+"λ "+str(count_alfa), end="")
        # entering into the vectors dictionary
        A_str.append(str(i))
        None
        count_a += 1
        count_alfa += 1
        else:
        else:
        else:
        first = False
        for j in keys_list:
        i = str(i)
        if int(i) < 0:
        if int(i) < 0:
        if int(vectors[j][i]) > 0 and count_first is False:
        matrix.append(int(vectors[j][i]))
        opred_matrix_3x3(matrix)
        print(" ", j, end=")")
        print("= ", end="")
        print("|", end="")
        print("The matrix is not 3x3, find the determinant yourself")
        print("For example: p 3 -2 4\n "+""*10+"g -2 1 3\n "+""*10+"r 7 -4 1 (in three dimensions)")
        print("Wrong number of characters! Print again\n")
        print()
        print()
        print(vectors[j][i], end=")")
        vectors[j[0]] = j[1:]
        x -= 1
    # input
    # output all
    # checks
    # intermediate things
    # let's make the negative intermediate things put in parentheses too
    # let's put negative numbers in brackets
    A_el1 = A[0]*A[4]*A[8]
    A_el2 = A[1]*A[5]*A[6]
    A_el3 = A[3]*A[2]*A[7]
    A_el4 = A[6]*A[4]*A[2]
    A_el5 = A[1]*A[3]*A[8]
    A_el6 = A[0]*A[5]*A[7]
    A_str=[]
    Ael = [A_el1, A_el2, A_el3, A_el4, A_el5, A_el6]
    count_a += 1
    count_alfa = 1
    count_first = True
    count_tringle += 1
    elif 0 == 1:
    else:
    else:
    for i in A:
    for i in A_str:
    for i in Ael:
    for i in x:
    for j in keys_list:
    for j in keys_list:
    for j in vectors[i]:
    global opred_A, out_A
    if dim == 3:
    if first:
    if len(j) != dim+1:
    j = input("")
    j = j.split()
    matrix = []
    opred_A = (A[0]*A[4]*A[8]+A[1]*A[5]*A[6]+A[3]*A[2]*A[7]-A[6]*A[4]*A[2]-A[1]*A[3]*A[8]-A[0]*A[5]*A[7])
    opred_matrix_3x3(matrix)
    out_A = []
    out_Ael = []
    print(" ", i, end="")
    print(" =",a[count_a])
    print(")")
    print("= ", end="")
    print("{}{}*{}*{} + {}*{}*{} + {}*{}*{} - {}*{}*{} - {}*{}*{} - {}*{}*{} =". format(out_A[0], out_A[4], out_A[8], out_A[1], out_A[5], out_A[6], out_A[3], out_A[2], out_A[7], out_A[6], out_A[4], out_A[2], out_A[1], out_A[3], out_A[8], out_A[0], out_A[5], out_A[7]), end= "")
    print("{}+{}+{}-{}-{}-{} = {}\n".format(out_Ael[0], out_Ael[1], out_Ael[2], out_Ael[3], out_Ael[4], out_Ael[5], opred_A))
    print("|")
    print("|", end="")
    print("Δ "+str(count_tringle))
    print("Enter the name of the basis vector and its values with spaces")
    print("The matrix is not 3x3, find the determinant yourself")
    print("⃗"+str(i),"= (",end="")
    print()
    return (A[0]*A[4]*A[8]+A[1]*A[5]*A[6]+A[3]*A[2]*A[7]-A[6]*A[4]*A[2]-A[1]*A[3]*A[8]-A[0]*A[5]*A[7])

# ------------ENTRY------------
# ------------Estage 1------------
# ------------Stage 2------------
# ------------Step 3------------
# dim - dimentional
# Entry
# just a triangle
# triangles with numbers
a = a.split()
a = input("By analogy, enter the data of the decomposable vector\for example: a 25 -15 14\n")
count_a = 0
count_a = 1
count_a2 = 1
count_alfa = 1
count_tringle = 1
def opred(A):
def opred_matrix_3x3(A):
dim = int(input("How many dimensions?\n"))
else:
first = True
for i in a[1:]:
for i in keys_list:
for i in x:
for i in x:
if dim == 3:
input()
keys_list = list(vectors.keys())
matrix = []
print("(number after λ - index λ)")
print(")\n")
print("1)given")
print("\nThe delta triangles")
print("Δ")
print("⃗"+a[0],"= (",end="")
vectors = {} # dictionary of all vectors
while count_tringle <= dim:
while x>0:
x = len(range(1,dim+1))
x = list(range(0,dim))

整理修复后的代码

以下是修复缩进、调整逻辑顺序、翻译注释后的完整代码:

# ------------入口------------
# ------------阶段1------------
# ------------阶段2------------
# ------------步骤3------------
# dim - 维度
# 入口
# 基础三角计算
# 带编号的三角计算

# 定义3阶矩阵行列式计算函数
def opred_matrix_3x3(A):
    # 计算行列式各项
    A_el1 = A[0] * A[4] * A[8]
    A_el2 = A[1] * A[5] * A[6]
    A_el3 = A[3] * A[2] * A[7]
    A_el4 = A[6] * A[4] * A[2]
    A_el5 = A[1] * A[3] * A[8]
    A_el6 = A[0] * A[5] * A[7]
    
    # 计算行列式结果
    opred_A = A_el1 + A_el2 + A_el3 - A_el4 - A_el5 - A_el6
    
    # 准备输出格式(处理负数加括号)
    out_A = []
    out_Ael = []
    for i in A:
        i_str = str(i)
        if int(i) < 0:
            out_A.append(f"({i_str})")
        else:
            out_A.append(i_str)
    
    for i in [A_el1, A_el2, A_el3, A_el4, A_el5, A_el6]:
        i_str = str(i)
        if int(i) < 0:
            out_Ael.append(f"({i_str})")
        else:
            out_Ael.append(i_str)
    
    # 打印行列式展开过程
    print("|", end="")
    for idx, num in enumerate(A):
        print(f" {num} ", end="|") if (idx+1) % 3 == 0 else print(f" {num} ", end="")
    print("\n= ", end="")
    print("{}{}*{}*{} + {}*{}*{} + {}*{}*{} - {}*{}*{} - {}*{}*{} - {}*{}*{} =".format(
        out_A[0], out_A[4], out_A[8], out_A[1], out_A[5], out_A[6],
        out_A[3], out_A[2], out_A[7], out_A[6], out_A[4], out_A[2],
        out_A[1], out_A[3], out_A[8], out_A[0], out_A[5], out_A[7]
    ), end="")
    print(f"{out_Ael[0]}+{out_Ael[1]}+{out_Ael[2]}-{out_Ael[3]}-{out_Ael[4]}-{out_Ael[5]} = {opred_A}\n")
    return opred_A

# 定义通用行列式计算入口
def opred(A):
    if len(A) == 9:
        return opred_matrix_3x3(A)
    else:
        print("矩阵不是3x3,请自行计算行列式")
        return None

# 主程序开始
# 获取维度
dim = int(input("请输入维度:\n"))
vectors = {}  # 存储所有基向量的字典

# 输入基向量
print("请输入基向量名称及其分量,用空格分隔")
print("示例:p 3 -2 4\n      g -2 1 3\n      r 7 -4 1(三维情况)")
x = dim
while x > 0:
    j = input().strip()
    j = j.split()
    if len(j) != dim + 1:
        print("输入格式错误!请重新输入\n")
        continue
    vectors[j[0]] = j[1:]
    x -= 1

keys_list = list(vectors.keys())

# 输入待分解向量
a = input("请参照示例输入待分解向量的数据\n示例:a 25 -15 14\n")
a = a.split()

# 初始化计数器
count_a = 0
count_a2 = 1
count_alfa = 1
count_tringle = 1
count_first = True

# 输出向量分解过程
print("⃗" + a[0], "= (", end="")
for i in a[1:]:
    print(i, end="")
    if count_a < dim - 1:
        print(", ", end="")
    count_a += 1
print(")\n")

print("(λ后的数字是λ的下标)")
print("\n1) 给定条件")
print("\nΔ三角计算")

# 处理3维情况的行列式计算
if dim == 3:
    matrix = []
    count_tringle = 1
    while count_tringle <= dim:
        print(f"Δ {count_tringle}")
        print("|", end="")
        count_first = True
        for j in keys_list:
            # 填充矩阵并打印
            for i in range(dim):
                val = int(vectors[j][i])
                matrix.append(val)
                if val > 0 and count_first is False:
                    print(f"+{val}", end="")
                else:
                    print(val, end="")
                count_first = False
            print(f"  {j})")
        # 添加待分解向量到矩阵
        count_first = True
        for i in a[1:]:
            val = int(i)
            matrix.append(val)
            if val > 0 and count_first is False:
                print(f"+{val}", end="")
            else:
                print(val, end="")
            count_first = False
        print("  =")
        
        # 计算行列式
        opred_result = opred(matrix)
        if opred_result is not None:
            print(f"λ{count_tringle} = {opred_result}\n")
        
        matrix.clear()
        count_tringle += 1
else:
    print("矩阵不是3x3,请自行计算行列式")

整理说明

  1. 调整代码顺序:将函数定义移至调用前,变量初始化移至使用前,修复逻辑顺序混乱问题
  2. 修复缩进错误:统一使用4空格缩进,修正所有条件、循环语句的缩进层级
  3. 清理冗余代码:删除重复的else、if语句,移除无意义的None语句
  4. 翻译注释与提示:将所有英文注释、输入输出提示翻译成中文,符合使用习惯
  5. 补充逻辑完整性:完善基向量输入的格式校验,补充向量分解的输出流程,确保程序可运行
  6. 优化输出格式:处理负数的括号显示,让行列式展开过程更清晰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:35:25