当距离/shi使用100时未获得预期输出

2024-04-19 18:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

输入/输出如下: 第1组: “abcde”“128” 预期产量/实际产量 “abcde”/“abcde”

第二组: “abcde”“100” 预期产量/实际产量 “EFGHI”/“«¬­®¯"

第三组: “你好,世界”“3” 预期产量/实际产量 “Khoor#Zruog”/“Khoor#Zruog”

设置1&;3正确返回,但2不正确。我认为这与我试图使用的字符集有关,但我不知道如何修复它

plainText = input("Enter text to encrypt: ")
distance = int(input("Enter number of offset: "))
code = ""
for ch in plainText:
    distance %= 56
    ordValue = ord(ch)
    cipherValue = ordValue + distance
    if cipherValue > ord('z'):
        cipherValue = ord('a') + distance -(ord('z') - ordValue + 1)
    code = code + chr(cipherValue)
print("distance= ",distance)
print(code)

Tags: inputcodechdistanceprint产量enterplaintext
1条回答
网友
1楼 · 发布于 2024-04-19 18:22:39

我可能不太清楚你想要什么,但我知道你包装得不对。这就是你想做的吗(它确实适用于您提供的示例。)

plainText = input("Enter text to encrypt: ")
distance = int(input("Enter number of offset: "))
code = ""
for ch in plainText:
    ordValue = ord(ch)
    cipherValue = (ordValue + distance) % 128
    code = code + chr(cipherValue)
print("distance= ",distance)
print(code)

相关问题 更多 >