Python Forum

Full Version: Problem with a simple script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys! I'm learning python from Kenneth A Lambert's book, and I'm stuck in this exercise. The book asks me to write a script that crypts a message using ASCIIs. I wrote two scripts, one to crypt the message and the other to encrypt it, but they just don't seem to work, if I get a result from one, the other is going to give me a different one.
Script 1
frase = input("Insert a text to crypt: ")
dist = int(input("Insert distance: "))
code = ""
for word in frase:
    value = ord(word)
    crypt = value + dist
    if codice > ord(chr(127)):
        codice = ord(chr(33)) + dist - (ord(chr(127)) - crypt)
    code += chr(crypt)
print("Your crypted code is : ", code)
Script 2
frase = input("Insert a text to decrypt it: ")
dist = int(input("Insert distance: "))
code = ""
for word in (frase):
    value = ord(word)
    crypt = value - dist
    if crypt < ord(chr(33)):
        crypt = ord(chr(127)) - (dist - (codice - ord(chr(33)))) 
    code += chr(crypt)
print("Your uncrypted message is: ", code)
It doesn't handle spaces correctly, because ord(space) is 32, but other wise it works for me. That is, after I converted all the 'codice' to 'crypt'. That was giving me lots of errors.

Note that ord(chr(x)) is always going to equal x, so it is not necessary.
Oh, thanks! Yeah, I first wrote that in Italian and then I translated it, guess I forgot to change some of the words.