值错误:chr()arg不在范围内(0x110000)

2024-06-16 10:58:01 发布

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

编写解码程序并继续运行

ValueError: chr() arg not in range(0x110000)

当我输入需要解码的字符串时。输入字符串为:

[2ea^W_`^k2eiWSd2fZSf2[2S_2gb2fa2`a2YaaV@

目前代码如下:

# String manipulation
# This program accepts a string and an integer
# then decodes the number of lines by a know decryption key

# Initialize the program and necessary variable
print("This progam can decode an encrypted by a known encryption key")
string=""
decoded_message=""
coded_message=""

# Prompting the used for input using a for loop to accept multiple lines
coded_message=input("What is the line to be decoded?")

# Using a for loop, the messges will be decrypted character
# at at time to its ASCII value then decrypted and converted
# back to text
for string in coded_message:
    converted_text=ord(string)
    decryption=(chr(converted_text-18))
    decoded_message+=decryption
# Output the decoded message
print("Your decrypted message is:",decoded_message)

我肯定我漏掉了一些简单的东西,但任何帮助都会很好


Tags: andtheto字符串textinmessagefor
2条回答

也许当你从原始的ascii值中减去18时,它就超出了范围,即小于0。

只需使用:

 coded_message=raw_input("What is the line to be decoded?")

而不是

 coded_message=input("What is the line to be decoded?")

这应该可以解决,所以

print("This progam can decode an encrypted by a known encryption key")
decoded_message=""

coded_message=raw_input("What is the line to be decoded?")

for string in coded_message:
    converted_text=ord(string)
    decryption=(chr(converted_text-18))
    decoded_message+=decryption

# Output the decoded message
print("Your decrypted message is:",decoded_message)

结果是:

('Your decrypted message is:', 'I SOLEMNLY SWEAR THAT I AM UP TO NO GOOD.')

相关问题 更多 >