【问题标题】:Python Encryption Code Is Not Encrypting All CharactersPython 加密代码未加密所有字符
【发布时间】:2015-12-09 14:16:18
【问题描述】:

我的代码没有在 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)

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

【问题讨论】:

  • result - 96 不能像现在这样生活,它应该被分配一个名字,如下面的行
  • 嗯,你期待什么?我在您的代码中看不到 print。应该显示什么? (不是说nospace是空的,循环不运行)
  • 搞定了,看看我上面的代码
  • 好的,谢谢。我现在正在尝试运行它...numbermathtext_read 的值是多少?
  • number的值为number = [],text_read为用户输入的文件

标签: python python-3.x encryption ascii


【解决方案1】:
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)

【讨论】:

  • 在你使用它的同时也可以摆脱多余的result = result... :)
  • 是的,它无害但没用。
  • 谢谢,它会加密,但是它会一一加密并显示出来:)
  • 如何让它不一个一个做,一次全部显示出来
  • 通过 print(string) 而不是 print(result_ascii)。如果您尝试过但没有成功,那很奇怪,因为它适用于 my machine
猜你喜欢
  • 2015-08-15
  • 1970-01-01
  • 2013-04-17
  • 2019-10-22
  • 2010-10-23
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多