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

Python代码缩进问题求助:添加141-142行后触发IndentationError

缩进错误(IndentationError)排查求助

在完成编程作业时,给main()函数添加以with开头的两行代码后,出现了IndentationError(缩进错误)。我已花费大量时间排查却找不到问题,朋友的相似代码却能正常运行。

我尝试把with相关的文件读取功能移到独立函数中,在main()里调用后还是出现同样的缩进错误。我的需求是通过with语句将指定文件的所有内容存入数组。

已尝试的解决步骤

  • 对比朋友的相似代码,对方代码可正常运行
  • 将with相关功能移至独立函数,main调用后仍报相同缩进错误

完整代码

#Imports
import sys
import os
from bitstring import BitArray

#Declaration of variables
op = ''
rt = ''
rs = ''
imm = ''
ans = ''
shamt = ''
funct = ''
comm = ''
RegDst = ''
ALUSrc = ''
MemtoReg = ''
RegWrite = ''
MemRead = ''
MemWrite = ''
branch = ''
ALUOp1 = ''
ALUOp2 = ''
zeroBit = ''
oper = ''

#Dictionaries
opDict = {'001000' : 'addi', '000000' : {'100000' : 'add', '100010' : 'sub'}}
regValues = [0, 0, 0, 0, 0, 0, 0, 0]
regMap = {'00000' : 0 , '00001' : 1, '00010' : 2, '00011' : 3, '00100' : 4, '00101' : 5, '00110' : 6, '00111' : 7}


#Main function

        
def program(request, newFile, newFileTwo, count):
    splitInput(request, newFile)
    ans = str(regValues).replace(",", "")
    ans = ans.replace("[","")
    ans = ans.replace("]","")
    ans = ans.replace(" ","|")  
    newFileTwo.write(str(count) + "|" + ans + '\n')

#Utility functions
def splitInput(inn, file):
    op = inn[0:6]
    if (op == '001000'):
        # I-Type
        rt = inn[6:11]
        rs = inn[11:16]
        imm = inn[16:32]
        b = BitArray(bin=imm)
        ans = b.int
        oper = 'addi'
        decideCtrl(oper, file)
        addi(rt, rs, ans)
    elif (op == '000000'):
        # R - Type
        rs = inn[6:11]
        rt = inn[11:16]
        rd = inn[16:21]
        shamt = inn[21:26]
        funct = inn[26:33].replace("\n", "")
        # Add / Sub
        if(opDict['000000'][funct] == 'add'):
            # Add command
            oper = 'add'
            decideCtrl(oper, file)
            add(rs,rt,rd)
        elif(opDict['000000'][funct] == 'sub'):
            # Sub Command
            oper = 'sub'
            decideCtrl(oper, file)
            sub(rd, rs, rt)

 
#Dedice on control signals
def decideCtrl(op, file):
    match op:
        case 'add':
            RegDst = '1'
            RegWrite = '1'
            ALUSrc = '0'
            MemtoReg = '0'
            RegWrite = '1'
            MemRead = '0'
            MemWrite = '0'
            branch = '0'
            ALUOp1 = '1'
            ALUOp2 = '0'
            comm = 'add'
            file.write(RegDst + ALUSrc + MemtoReg + RegWrite + MemRead + MemWrite + branch + ALUOp1 + ALUOp2 +'\n')
        case 'sub':
            RegDst = '1'
            RegWrite = '1'
            ALUSrc = '0'
            MemtoReg = '0'
            RegWrite = '1'
            MemRead = '0'
            MemWrite = '0'
            branch = '0'
            ALUOp1 = '1'
            ALUOp2 = '0'
            comm = 'sub'
            file.write(RegDst + ALUSrc + MemtoReg + RegWrite + MemRead + MemWrite + branch + ALUOp1 + ALUOp2 + '\n')
        case 'addi':
            RegDst = '0'
            RegWrite = '1'
            ALUSrc = '1'
            MemtoReg = '0'
            RegWrite = '1'
            MemRead = '0'
            MemWrite = '0'
            branch = '0'
            ALUOp1 = '0'
            ALUOp2 = '0'
            comm = 'addi'
            file.write(RegDst + ALUSrc + MemtoReg + RegWrite + MemRead + MemWrite + branch + ALUOp1 + ALUOp2 + '\n')

#Commands
def add(rs, rt, rd):
    regValues[regMap[rd]] = regValues[regMap[rs]] + regValues[regMap[rt]]

def sub(rd, rs, rt):
    regValues[regMap[rd]] = regValues[regMap[rs]] - regValues[regMap[rt]]

def addi(rt, rs, imm):
    regValues[regMap[rs]] = regValues[regMap[rt]] + imm
    
   

def main():
    PCount = 65536
    count = 0
    # inputName = str(sys.argv[1])
    # F = open(inputName, "r")

    with open(filename, "r") as file:
        storedLines = file.read().splitlines()

    newFile = open("outputOne.txt", "w")
    newFileTwo = open("outputTwo.txt", "w")

    
    
    ans = str(regValues).replace(",", "")
    ans = ans.replace("[","")
    ans = ans.replace("]","")
    ans = ans.replace(" ","|")  
    newFileTwo.write(str(PCount) + "|" + ans + '\n')
    
    # for comms in F:
    #   if (count == 100):
    #       F.close()
    #       newFile.close()
    #       newFileTwo.close()
    #       exit()
    #   PCount = PCount + 4
    #   program(comms, newFile, newFileTwo, PCount)
    #   count = count + 1


    
    #F.close()
    newFile.close()
    newFileTwo.close()  


# Use open file to store the lines with their indicies
# When beq or bne compare and if it's true -> grab the label imm -> convert to decimal
# -> divide by 4 to get actual line

# Ad

# While i less than size of array of lines go one by one and do logic
#   storedLines[i] - line of a code in a file. Do logic to each [i]
# Add internal pc counter (intpc=0). By each iteration of i intpc goes +4
# at the end  of while loop internalPC = 4
# i == internalPC // 4

# App Entry Point
if __name__ == "__main__":
    main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 08:45:32