Python Forum
Problem with a simple script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with a simple script
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Oh, thanks! Yeah, I first wrote that in Italian and then I translated it, guess I forgot to change some of the words.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple function problem stereokim123 5 3,250 Aug-26-2021, 04:44 PM
Last Post: naughtyCat
  I have a simple problem ahmed 5 2,506 Jul-17-2021, 02:50 PM
Last Post: ahmed
  Simple problem with functions and returns danlopek14q 10 6,705 Mar-17-2021, 05:32 PM
Last Post: danlopek14q
  Please help with this simple tic tac toe script ErtYwek 2 1,897 Mar-06-2020, 06:42 PM
Last Post: ndc85430
  Easy script problem Niko047 5 3,910 Jul-22-2017, 09:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020