简体   繁体   English

Python 代码在打印语句期间/之后停止输出,但同一部分代码在作为自己的程序隔离时工作。 这是怎么回事?

[英]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?

I am trying to implement my own version of Hamming code with even parity (as shownhere , except I'm starting counting the bits from left to right , rather than right to left) in Python.我正在尝试在 Python 中实现我自己的汉明码版本,具有校验(如图所示,除了我开始从左到右而不是从右到左计算位)。

My code is as follows:我的代码如下:

# 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)

The transmitted codeword in this case is 1100011.在这种情况下传输的码字是 1100011。

Each bit whose ordinal position is a power of 2 (1, 2, 4, 8) is a check bit and forces the parity of some 'collection' of bits including itself.每个位的序号 position 是 2 的幂(1、2、4、8)是一个校验位,并强制对包括其自身在内的一些“集合”位进行奇偶校验。 Parity may be forced to be either even or odd.奇偶校验可能被强制为偶数或奇数。

A data bit contributes to the parity of all the bits in its decomposition into a sum of powers of 2 .一个数据位有助于将其分解为 2 的幂和中的所有位的奇偶性。 For example:例如:
3 = 2 + 1 3 = 2 + 1
7 = 4 + 2 + 1 7 = 4 + 2 + 1
11 = 8 + 2 + 1 11 = 8 + 2 + 1
Making a list of these:列出这些:
1 is contributed to by 3, 5, 7, 9, 11 1 由 3、5、7、9、11 贡献
2 is contributed to by 3, 6, 7, 10, 11 2 由 3, 6, 7, 10, 11 贡献
4 is contributed to by 5, 6, 7 4 由 5、6、7 贡献
8 is contributed to by 9, 10, 11 8 由 9、10、11 贡献
We then put the data bits in their positions and calculate each check bit.然后我们将数据位放在它们的位置并计算每个校验位。
For example, for 1100011, using even parity:例如,对于 1100011,使用校验:
在此处输入图像描述

1 is contributed to by 3, 5, 7, 9, 11, and if we look at these bits for check 1, we have a 1 at bits 3, 5, and 11, which is odd (1 + 1 + 1 = 3), so since we want even parity, we set the bit 1 to 1. Skipping to 8, we see that 8 is contributed to by 9, 10, 11, and if we look at these bits for check 4, we have a 1 at bits 10 and 11, which is even (1 + 1 = 2), so since want even parity, we set the bit 8 to 0. This is how we get 11111000011. 1 由 3、5、7、9、11 贡献,如果我们查看这些位进行检查 1,我们在位 3、5 和 11 处有一个 1,这是奇数 (1 + 1 + 1 = 3 ),所以由于我们想要偶校验,我们将位 1 设置为 1。跳到 8,我们看到 8 由 9、10、11 贡献,如果我们查看这些位以进行检查 4,我们有一个 1在第 10 位和第 11 位,这是偶数 (1 + 1 = 2),所以由于想要偶校验,我们将第 8 位设置为 0。这就是我们得到 11111000011 的方式。

The code above should produce 11111000011 from the input 1100011.上面的代码应该从输入 1100011 生成 11111000011。

As you can see in the code, I've added a bunch of print statements for debugging.正如您在代码中看到的,我添加了一堆用于调试的打印语句。 The problem is that this python code just randomly stops outputting anything at问题是这个 python 代码只是随机停止输出任何东西

The powers of 2 that sum to 3 are 
1
2

as seen here:如此处所示:

在此处输入图像描述

It's just stuck there.它只是卡在那里。

This is very perplexing, because if I execute only the part of the code that contains this print statement, it seems to work fine:这很令人困惑,因为如果我只执行包含此打印语句的部分代码,它似乎可以正常工作:

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)

在此处输入图像描述 在此处输入图像描述

I've only just started learning Python, so I honestly have no idea what's going wrong.我才刚刚开始学习 Python,所以我真的不知道出了什么问题。 Why isn't my code working, and how do I fix it?为什么我的代码不起作用,我该如何解决?


EDIT1编辑1

I think I fixed the indentation error with z <<= 1 , as Tim Roberts suggested, but now I get another error.正如蒂姆罗伯茨建议的那样,我想我用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)

Error:错误:

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

Again, the other program doesn't have this error:同样,另一个程序没有此错误:

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

EDIT2编辑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)

Error:错误:

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

EDIT3编辑3

Ok, I think I got it!好的,我想我明白了!

# 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)

Output: Output:

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

... skip to 8 == 8 : ...跳到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

And so we have 11111000011, as required!所以我们有 11111000011,根据需要!


EDIT4编辑4

I cleaned up the last part by changing it back to我清理了最后一部分,将其改回

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

This seems to clean up the output:这似乎清理了 output:

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

You're going to kick yourself.你会踢自己的。 When you Ctrl-C to terminate the program, did you notice where it was looping?当你 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

If you'll look at that loop, you'll see that z never increments, so it loops forever.如果你看一下那个循环,你会发现z永远不会增加,所以它永远循环。 That's because line 63:那是因为第 63 行:

    z <<= 1

is indented in one too many indents.缩进一个太多的缩进。 If you fix that, it moves on.如果你解决了这个问题,它会继续前进。 It does eventually get a different error, but that will be for another question.它最终确实会得到一个不同的错误,但这将是另一个问题。

EDIT编辑
Other problems.其他问题。 In determineCheckBits , you are treating res as if it is a list of ints, trying to assign elements by index.determineCheckBits中,您将res视为整数列表,尝试按索引分配元素。 It is not, it is a string.它不是,它是一个字符串。 So, at the beginning, change:所以,一开始,改变:

    res = data

to

    res = list(data)

Towards the bottom, change向下,改变

        res[i] = 1

to

        res[i] = '1'

and do the same 4 lines later with 0. Then, change the last 5 lines to:并稍后使用 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

and I think you will have happiness.我想你会幸福的。

Would you be upset if I did this in 28 lines?如果我在 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM