[Solved] Python ValueError: chr() arg not in range(0x110000)


There’s a couple of tweaks I had to make to get your code to work, here’s a working version:

import enchant
message_decrypt= input("Enter the message you want to decrypt: ")
key= 0
def caesar_hack(message_decrypt,key):
    final_message=""
    d= enchant.Dict("en.US")
    f= d.check(message_decrypt)
    while f== False:
        for characters in message_decrypt:
            if ord(characters)<=90:
                if ord(characters)-key<ord("A"):
                    final_message= final_message+ chr(ord(characters)-key+26)  # The additional 26 should be here, not below
                else:
                    final_message= final_message+ chr(ord(characters)-key)
            else:
                if ord(characters)-key<ord("a"):
                    final_message=final_message+chr(ord(characters)-key+26)  # The additional 26 should be here, not below
                else:
                    final_message= final_message+chr(ord(characters)-key)
        key=key+1
        f= d.check(final_message)  # Check should be on final_message, not message_decrypt
        if not f:
            final_message = ""  # Need to reset the final_message if not matched
    else:
        print(final_message)
caesar_hack(message_decrypt, key)

I’ve commented the main changes I made. One key one was checking final_message in the loop, not message_decrypt (and resetting it again for the next loop if no match).

The other was that your addition of 26 to the character ordinal if it was out of range needed to be moved. Without doing that, it was generating non-printable characters so the check was failing with an enchant error.

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