Python加密代码不加密所有字符

时间:2015-12-09 14:16:18

标签: python python-3.x encryption ascii

我的代码没有在Python中显示。有什么问题?

offset_factor = math.floor(number[0]+number[1]+number[2]+number[3]+number[4]+number[5]+number[6]+number[7])
    total = offset_factor
    total = total / 8 - 32
    total = round(total)

    print("This is your offset factor: ")
    print(total)
    string = ''.join(str(e) for e in text_read)
    nospace = string.replace(" ", "")
    print(nospace)

    for b in (nospace):
        string = ""
        ASCII = ord(b)
        result = ASCII + total
        if result > 126:
            result - 96
        else:
            result = result
            result_ascii = chr(result)
            string += result_ascii

    for b in nospace:
        string = ""
        ASCII = ord(b)
        result = ASCII + total
        if result > 126:
            result - 96
        else:
            result = result
            result_ascii = chr(result)
            string += result_ascii
            print(result_ascii)

此代码没有显示,我不知道为什么不,它只加密八个字符,但我不知道如何加密所有字符。

1 个答案:

答案 0 :(得分:0)

for b in nospace:
    string = ""
    ASCII = ord(b)
    result = ASCII + total
    if result > 126:
        result - 96
    else:
        result = result
        result_ascii = chr(result)
        string += result_ascii
        print(result_ascii)

我在这里看到了几个问题。

  • 如果希望string在循环结束时包含result_ascii的累计值,则不应在每次迭代中将其重置为空字符串。你应该只在开始时做一次。
  • 如果您希望result在大于126时变小,则必须将result - 96分配给某些内容。
  • 如果您希望string +=块执行if块,则不应使用else块。
  • 如果您希望输出显示在一行中,请不要单独print每个字符。

尝试:

string = ""
for b in (nospace):
    ASCII = ord(b)
    result = ASCII + total
    if result > 126:
        result = result - 96
    result = result
    result_ascii = chr(result)
    string += result_ascii
print(string)