Python 代码停止输出 during/after print 语句,但同一部分代码在独立为自己的程序时可以正常工作。这是怎么回事?

Python code stops outputting during/after print statement, but the same part of code works when isolated as its own program. What's going on?

我正在尝试使用 even 奇偶校验(如图 here 所示,除了 我开始计算Python.

中从左到右的位 ,而不是从右到左)

我的代码如下:

# Python program to demonstrate Hamming code
  
  
def calcCheckBits(m):
  
    # Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
    # Iterate over 0 ... m and return the value that satisfies the equation.
  
    for i in range(m):
        if(2**i >= m + i + 1):
            print("Number of redundant bits: " + str(i))
            return i


def posCheckBits(data, r):
  
    # Check bits are placed at the positions that correspond to the power of 2.
    j = 0
    k = 1
    m = len(data)
    res = ''
  
    # If position is power of 2, then insert '0'; else, append the data.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            print("If " + str(i) + " == " + str(2**j))
            res = res + '0'
            print("check bit placed at position " + str(i))
            print("res = " + res)
            j += 1
        else:
            res = res + data[k - 1]
            print("appending data at position " + str(i))
            print("res = " + res)
            k += 1
    
    print("res: " + res)
    return res


def determineCheckBits(data, r):
  
    # Check bits are placed at the positions that correspond to powers of 2.
    j = 0
    m = len(data)
    res = data
    activatedBitCounter = 0
  
    # If position i is a power of 2, then find the value of the check bit; else, do nothing.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            contributedToBy = [] # the bits that have i in their sum of powers of 2
            print("If " + str(i) + " == " + str(2**j))
            # Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
            print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
            for y in range(3, m + r + 1):
                powers = []
                z = 1
                while z <= y:
                    if z & y:
                        powers.append(z)
                        z <<= 1
                print("The list of powers is " + str(powers))
                print("The powers of 2 that sum to " + str(y) + " are ")
                for l in range(len(powers)):
                    print(powers[l])
                if(i in powers):
                    contributedToBy.append(y)
            print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
            for l in range(len(contributedToBy)):
                print(contributedToBy[l])
            for l in contributedToBy:
                if(int(res[l - 1]) == 1):
                    activatedBitCounter += 1
            # Check if the number of activated bits is odd.
            # If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
            if((activatedBitCounter % 2) != 0):
                res[i] = 1
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
                print("res is now " + str(res))
            else:
                res[i] = 0
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
                print("res is now " + str(res))
            activatedBitCounter = 0 # reset counter
            j += 1 # Append j by 1 to move onto next power
        else:
            print("else, position " + str(i) + " is not a power of 2, so res = " + res)
    
    print("Final res value from function determineCheckBits: " + res)
    return res


# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
  
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
  
# Determine the positions of check bits
arr = posCheckBits(data, r)

# Determine the check bits
arr = determineCheckBits(arr, r)

本例中传输的码字为1100011。

序号位置为 2 的幂(1、2、4、8)的每个位都是校验位,并强制某些 'collection' 位(包括其自身)进行奇偶校验。奇偶校验可能被强制为偶数或奇数。

数据位在其分解为 2 的幂和时有助于所有位的奇偶校验。例如:
3 = 2 + 1
7 = 4 + 2 + 1
11 = 8 + 2 + 1
列出这些:
1 是由 3, 5, 7, 9, 11 贡献的
2 由 3, 6, 7, 10, 11 贡献
4 是由 5, 6, 7 贡献的
8 是由 9, 10, 11
贡献的 然后我们将数据位放在它们的位置并计算每个校验位。
例如,对于 1100011,使用 even 奇偶校验:

1 由 3、5、7、9、11 贡献,如果我们查看这些位以进行检查 1,我们在第 3、5 和 11 位有一个 1,这是奇数 (1 + 1 + 1 = 3),所以因为我们想要偶校验,所以我们将位 1 设置为 1。跳到 8,我们看到 8 是由 9、10、11 贡献的,如果我们查看这些位以进行校验 4 ,我们在第 10 位和第 11 位有一个 1,它是偶数 (1 + 1 = 2),所以由于想要偶校验,我们将第 8 位设置为 0。这就是我们如何得到 11111000011.

上面的代码应该从输入 1100011 中产生 11111000011。

正如您在代码中看到的,我添加了一堆用于调试的打印语句。问题是这个 python 代码只是在

随机停止输出任何东西
The powers of 2 that sum to 3 are 
1
2

如此处所示:

它只是卡在那里。

这很令人费解,因为如果我只执行包含此 print 语句的代码部分,它似乎工作正常:

def testPowers(m, r):
# If position i is power of 2, then find the value of the parity bit; else, do nothing.
    j = 0
    for i in range(1, m + r + 1):
        if(i == 2**j):
            contributedToBy = [] # the bits that have i in their sum of powers of 2
            print("If " + str(i) + " == " + str(2**j))
            # Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
            print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
            for y in range(3, m + r + 1):
                powers = []
                z = 1
                while z <= y:
                    if z & y:
                        powers.append(z)
                    z <<= 1
                print("The powers of 2 that sum to " + str(y) + " are ")
                for l in range(len(powers)):
                    print(powers[l])
                if(i in powers):
                    contributedToBy.append(y)
            print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
            for l in range(len(contributedToBy)):
                print(contributedToBy[l])
                
m = 7
r = 4
testPowers(m, r)

我才刚刚开始学习 Python,所以老实说我不知道​​出了什么问题。为什么我的代码不起作用,我该如何解决?


编辑1

我想我按照 Tim Roberts 的建议修复了 z <<= 1 的缩进错误,但现在我又遇到了另一个错误。

# Python program to demonstrate Hamming code
  
  
def calcCheckBits(m):
  
    # Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
    # Iterate over 0 ... m and return the value that satisfies the equation.
  
    for i in range(m):
        if(2**i >= m + i + 1):
            print("Number of redundant bits: " + str(i))
            return i


def posCheckBits(data, r):
  
    # Check bits are placed at the positions that correspond to the power of 2.
    j = 0
    k = 1
    m = len(data)
    res = ''
  
    # If position is power of 2, then insert '0'; else, append the data.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            print("If " + str(i) + " == " + str(2**j))
            res = res + '0'
            print("check bit placed at position " + str(i))
            print("res = " + res)
            j += 1
        else:
            res = res + data[k - 1]
            print("appending data at position " + str(i))
            print("res = " + res)
            k += 1
    
    print("res: " + res)
    return res


def determineCheckBits(data, r):
  
    # Check bits are placed at the positions that correspond to powers of 2.
    j = 0
    m = len(data)
    res = data
    activatedBitCounter = 0
  
    # If position i is a power of 2, then find the value of the check bit; else, do nothing.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            contributedToBy = [] # the bits that have i in their sum of powers of 2
            print("If " + str(i) + " == " + str(2**j))
            # Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
            print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
            for y in range(3, m + r + 1):
                powers = []
                z = 1
                while z <= y:
                    if z & y:
                        powers.append(z)
                    z <<= 1
                print("The list of powers is " + str(powers))
                print("The powers of 2 that sum to " + str(y) + " are ")
                for l in range(len(powers)):
                    print(powers[l])
                if(i in powers):
                    contributedToBy.append(y)
            print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
            for l in range(len(contributedToBy)):
                print(contributedToBy[l])
            for l in contributedToBy:
                if(int(res[l - 1]) == 1):
                    activatedBitCounter += 1
            # Check if the number of activated bits is odd.
            # If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
            if((activatedBitCounter % 2) != 0):
                res[i] = 1
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
                print("res is now " + str(res))
            else:
                res[i] = 0
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
                print("res is now " + str(res))
            activatedBitCounter = 0 # reset counter
            j += 1 # Append j by 1 to move onto next power
        else:
            print("else, position " + str(i) + " is not a power of 2, so res = " + res)
    
    print("Final res value from function determineCheckBits: " + res)
    return res


# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
  
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
  
# Determine the positions of check bits
arr = posCheckBits(data, r)

# Determine the check bits
arr = determineCheckBits(arr, r)

错误:

appending data at position 11
res = 00101000011
res: 00101000011
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are 
1
2
The list of powers is [4]
The powers of 2 that sum to 4 are 
4
The list of powers is [1, 4]
The powers of 2 that sum to 5 are 
1
4
The list of powers is [2, 4]
The powers of 2 that sum to 6 are 
2
4
The list of powers is [1, 2, 4]
The powers of 2 that sum to 7 are 
1
2
4
The list of powers is [8]
The powers of 2 that sum to 8 are 
8
The list of powers is [1, 8]
The powers of 2 that sum to 9 are 
1
8
The list of powers is [2, 8]
The powers of 2 that sum to 10 are 
2
8
The list of powers is [1, 2, 8]
The powers of 2 that sum to 11 are 
1
2
8
The list of powers is [4, 8]
The powers of 2 that sum to 12 are 
4
8
The list of powers is [1, 4, 8]
The powers of 2 that sum to 13 are 
1
4
8
The list of powers is [2, 4, 8]
The powers of 2 that sum to 14 are 
2
4
8
The list of powers is [1, 2, 4, 8]
The powers of 2 that sum to 15 are 
1
2
4
8
The bits that have 1 in their sum of powers of 2 are 
3
5
7
9
11
13
15
Traceback (most recent call last):
  File "/Users/x/testMultiParts.py", line 106, in <module>
    arr = determineCheckBits(arr, r)
  File "/Users/x/testMultiParts.py", line 73, in determineCheckBits
    if(int(res[l - 1]) == 1):
IndexError: string index out of range

同样,另一个程序没有这个错误:

If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The powers of 2 that sum to 3 are 
1
2
The powers of 2 that sum to 4 are 
4
The powers of 2 that sum to 5 are 
1
4
The powers of 2 that sum to 6 are 
2
4
The powers of 2 that sum to 7 are 
1
2
4
The powers of 2 that sum to 8 are 
8
The powers of 2 that sum to 9 are 
1
8
The powers of 2 that sum to 10 are 
2
8
The powers of 2 that sum to 11 are 
1
2
8
The bits that have 1 in their sum of powers of 2 are 
3
5
7
9
11

编辑2

# Python program to demonstrate Hamming code
  
  
def calcCheckBits(m):
  
    # Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
    # Iterate over 0 ... m and return the value that satisfies the equation.
  
    for i in range(m):
        if(2**i >= m + i + 1):
            print("Number of redundant bits: " + str(i))
            return i


def posCheckBits(res, r):
  
    # Check bits are placed at the positions that correspond to the power of 2.
    j = 0
    k = 1
    m = len(data)
    res = ''
  
    # If position is power of 2, then insert '0'; else, append the data.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            print("If " + str(i) + " == " + str(2**j))
            res = res + '0'
            print("check bit placed at position " + str(i))
            print("res = " + res)
            j += 1
        else:
            res = res + data[k - 1]
            print("appending data at position " + str(i))
            print("res = " + res)
            k += 1
    
    print("res: " + res)
    return res


def determineCheckBits(res, r):
  
    # Check bits are placed at the positions that correspond to powers of 2.
    j = 0
    res = list(data)
    m = len(res)
    activatedBitCounter = 0
    
    print("At the beginning, res is " + str(res))
    print("At the beginning, m is " + str(m))
    # If position i is a power of 2, then find the value of the check bit; else, do nothing.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            contributedToBy = [] # the bits that have i in their sum of powers of 2
            print("If " + str(i) + " == " + str(2**j))
            # Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
            print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
            for y in range(3, m + r + 1):
                powers = []
                z = 1
                while z <= y:
                    if z & y:
                        powers.append(z)
                    z <<= 1
                print("The list of powers is " + str(powers))
                print("The powers of 2 that sum to " + str(y) + " are ")
                for l in range(len(powers)):
                    print(powers[l])
                if(i in powers):
                    contributedToBy.append(y)
            print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
            for l in range(len(contributedToBy)):
                print(contributedToBy[l])
            for l in contributedToBy:
                if(int(res[l - 1]) == 1):
                    activatedBitCounter += 1
            # Check if the number of activated bits is odd.
            # If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
            if((activatedBitCounter % 2) != 0):
                res[i] = '1'
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
                print("res is now " + str(res))
            else:
                res[i] = '0'
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
                print("res is now " + str(res))
            activatedBitCounter = 0 # reset counter
            j += 1 # Append j by 1 to move onto next power
        else:
            print("else, position " + str(i) + " is not a power of 2, so res = " ''.join(res))
    res = ''.join(res)
    print("Final res value from function determineCheckBits: " + res)
    return res


# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
  
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
  
# Determine the positions of check bits
arr = posCheckBits(data, r)

# Determine the check bits
arr = determineCheckBits(arr, r)

错误:

res = 00101000011
res: 00101000011
At the beginning, res is ['1', '1', '0', '0', '0', '1', '1']
At the beginning, m is 7
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are 
1
2
The list of powers is [4]
The powers of 2 that sum to 4 are 
4
The list of powers is [1, 4]
The powers of 2 that sum to 5 are 
1
4
The list of powers is [2, 4]
The powers of 2 that sum to 6 are 
2
4
The list of powers is [1, 2, 4]
The powers of 2 that sum to 7 are 
1
2
4
The list of powers is [8]
The powers of 2 that sum to 8 are 
8
The list of powers is [1, 8]
The powers of 2 that sum to 9 are 
1
8
The list of powers is [2, 8]
The powers of 2 that sum to 10 are 
2
8
The list of powers is [1, 2, 8]
The powers of 2 that sum to 11 are 
1
2
8
The bits that have 1 in their sum of powers of 2 are 
3
5
7
9
11
Traceback (most recent call last):
  File "/Users/x/testMultiParts.py", line 108, in <module>
    arr = determineCheckBits(arr, r)
  File "/Users/x/testMultiParts.py", line 75, in determineCheckBits
    if(int(res[l - 1]) == 1):
IndexError: list index out of range

编辑3

好的,我想我明白了!

# Python program to demonstrate Hamming code
  
  
def calcCheckBits(m):
  
    # Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
    # Iterate over 0 ... m and return the value that satisfies the equation.
  
    for i in range(m):
        if(2**i >= m + i + 1):
            print("Number of redundant bits: " + str(i))
            return i


def posCheckBits(res, r):
  
    # Check bits are placed at the positions that correspond to the power of 2.
    j = 0
    k = 1
    m = len(data)
    res = ''
  
    # If position is power of 2, then insert '0'; else, append the data.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            print("If " + str(i) + " == " + str(2**j))
            res = res + '0'
            print("check bit placed at position " + str(i))
            print("res = " + res)
            j += 1
        else:
            res = res + data[k - 1]
            print("appending data at position " + str(i))
            print("res = " + res)
            k += 1
    
    print("res: " + res)
    return res


def determineCheckBits(arr, r):
  
    # Check bits are placed at the positions that correspond to powers of 2.
    j = 0
    res = list(arr)
    m = len(data)
    activatedBitCounter = 0
    
    print("At the beginning, res is " + str(res))
    print("At the beginning, m is " + str(m))
    # If position i is a power of 2, then find the value of the check bit; else, do nothing.
    for i in range(1, m + r + 1):
        if(i == 2**j):
            contributedToBy = [] # the bits that have i in their sum of powers of 2
            print("If " + str(i) + " == " + str(2**j))
            # Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
            print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
            for y in range(3, m + r + 1):
                powers = []
                z = 1
                while z <= y:
                    if z & y:
                        powers.append(z)
                    z <<= 1
                print("The list of powers is " + str(powers))
                print("The powers of 2 that sum to " + str(y) + " are ")
                for l in range(len(powers)):
                    print(powers[l])
                if(i in powers):
                    contributedToBy.append(y)
            print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
            for l in range(len(contributedToBy)):
                print(contributedToBy[l])
            for l in contributedToBy:
                print("contributedToBy is " + str(contributedToBy))
                print("res is " + str(res))
                print("res[l - 1] is " + res[l - 1])
                if(int(res[l - 1]) == 1):
                    activatedBitCounter += 1
            # Check if the number of activated bits is odd.
            # If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
            if((activatedBitCounter % 2) != 0):
                res[i - 1] = '1'
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
                print("res is now " + str(res))
            else:
                res[i - 1] = '0'
                print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
                print("res is now " + str(res))
            activatedBitCounter = 0 # reset counter
            j += 1 # Append j by 1 to move onto next power
        else:
            print("else, position " + str(i) + " is not a power of 2, so res = " ''.join(res))
    res = ''.join(res)
    print("Final res value from function determineCheckBits: " + res)
    return res


# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
  
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
  
# Determine the positions of check bits
arr = posCheckBits(data, r)

# Determine the check bits
arr = determineCheckBits(arr, r)

输出:

res = 00101000011
res: 00101000011
At the beginning, res is ['0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '1']
At the beginning, m is 7
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are 
1
2

...跳到8 == 8:

If 8 == 8
Now finding the values where 8 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are 
1
2
The list of powers is [4]
The powers of 2 that sum to 4 are 
4
The list of powers is [1, 4]
The powers of 2 that sum to 5 are 
1
4
The list of powers is [2, 4]
The powers of 2 that sum to 6 are 
2
4
The list of powers is [1, 2, 4]
The powers of 2 that sum to 7 are 
1
2
4
The list of powers is [8]
The powers of 2 that sum to 8 are 
8
The list of powers is [1, 8]
The powers of 2 that sum to 9 are 
1
8
The list of powers is [2, 8]
The powers of 2 that sum to 10 are 
2
8
The list of powers is [1, 2, 8]
The powers of 2 that sum to 11 are 
1
2
8
The bits that have 8 in their sum of powers of 2 are 
8
9
10
11
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 0
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 0
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 1
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 1
activatedBitCounter is 2 so set bit 8 of res to 0
res is now ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 91 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 1 is not a power of 2, so res = 1
else, position 101 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 1 is not a power of 2, so res = 1
else, position 111 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 1 is not a power of 2, so res = 1
Final res value from function determineCheckBits: 11111000011

所以我们需要 11111000011!


编辑4

我清理了最后一部分,将其改回

else:
            print("else, position " + str(i) + " is not a power of 2, so res = " + str(res))

这似乎清理了输出:

res is now ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 9 is not a power of 2, so res = ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 10 is not a power of 2, so res = ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 11 is not a power of 2, so res = ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
Final res value from function determineCheckBits: 11111000011

你会踢自己的。当你Ctrl-C终止程序时,你有没有注意到它在循环的地方?

The list of powers is [1, 2]
The powers of 2 that sum to 3 are 
1
2
^CTraceback (most recent call last):
  File "x.py", line 107, in <module>
    arr = determineCheckBits(arr, r)
  File "x.py", line 60, in determineCheckBits
    while z <= y:
KeyboardInterrupt

如果您查看该循环,您会发现 z 从不递增,因此它会永远循环下去。那是因为第 63 行:

    z <<= 1

缩进了太多的缩进。如果你解决了这个问题,它就会继续前进。它最终会得到一个不同的错误,但那将是另一个问题。

编辑
其他问题。在 determineCheckBits 中,您将 res 视为一个整数列表,试图按索引分配元素。它不是,它是一个字符串。所以,一开始,改成:

    res = data

    res = list(data)

向底部,改变

        res[i] = 1

        res[i] = '1'

并在后面用 0 执行相同的 4 行。然后,将最后 5 行更改为:

        else:
            print("else, position " + str(i) + " is not a power of 2, so res = " + ''.join(res))
    
    res = ''.join(res)
    print("Final res value from function determineCheckBits: " + res)
    return res

我想你会幸福的。

如果我在 28 行中这样做,你会不高兴吗?

data = '1100011'

bits = list(int(i) for i in data)
print(bits)

outbits = []
for i in range(1,99):
    if bin(i).count('1') == 1:
        outbits.append(0)
    else:
        outbits.append(bits.pop(0))
    if not bits:
        break

bits = outbits
print(bits)

ii = 1
while ii < len(bits):
    sumx = 0
    for i in range(len(bits)):
        if i != ii-1 and (i+1) & ii:
            sumx ^= bits[i]
    bits[ii-1] = sumx
    ii <<= 1

print(bits)