2016-06-29 17 views
0

デコードプログラムを作成し、デコードする必要がある文字列を入力するときにValueError:chr()argを範囲(0x110000)に入れないでください。ValueError:chr()argが範囲外(0x110000)

入力文字列がある:まま現在次のように

[2ea^W_`^k2eiWSd2fZSf2[2S_2gb2fa2`[email protected] 

コードは次のとおりです。

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

私は、単純な何かが欠けていますが、任意のヘルプは素晴らしいことだと確信している

答えて

0

たぶん、それは範囲の外に出ます、つまり、0未満です。

+0

これが問題だったことが分かります。私たちがテストに与えられたメッセージには、そこには存在しないはずのスペースがありました。 – Wyatt

1

ちょうど使用:

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) 

だから、結果は、修正する必要があります:あなたは、元のASCII値から18を引いたときに

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

これがどのような違いをもたらすのか説明しますか? –

+0

raw_input()は文字列を返し、input()は入力をPython式として実行しようとします。つまり、raw_inputにエンコードエラーはありません。 – dmitryro

+1

またはPython3を使用してください。既に$ CURRENTYEARです。 – o11c