Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy script problem
#1
Hey guys, it's me again. As an exercise, I was trying to write a script that "encrypts" a message crypted using  distance x (Caesar's code) and ASCII   code( 32 to 127). I wanted to write a script that prints all possibilities (95) and see which one actually makes sense, because that would be the "crypted" message. The code I wrote is the following
phrase = input("Write here the frase to encrypt -> ")
code = ""
n = 32
for tryy in range(95):
   for word in phrase:
       crypt = ord(word)
       crypt_1 = crypt + n

   if ord(word) > 127:
           crypt_1 = 32 + n - (127 - crypt)
   n += 1
   code += chr(crypt_1) 
   print("Your encrypted code could be one of the following: ", code, "\n")
But instead of giving me all the encrypted codes it just runs random digits, and the output keeps getting bigger.
Reply
#2
It's mainly an indentation problem. You have an outer loop that loops through the distances. You have an inner loop that loops through the characters in the plain text (you call that word, which is confusing, because it's just a character).

Your output is getting longer and longer because you never reset it. You want to reset code each time you loop to a new distance. You check to see if the character is over 127, but you only do that at the end of the loop over the phrase, so it's only checking the last character in the phrase. Likewise, you only add to the output (code) at the end of the loop over the phrase. So you are only outputting the last character for each distance.

Also, your correction for the "word" being over 127 is wrong. All you need to do is subtract 95.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-22-2017, 01:10 PM)ichabod801 Wrote: It's mainly an indentation problem. You have an outer loop that loops through the distances. You have an inner loop that loops through the characters in the plain text (you call that word, which is confusing, because it's just a character).

Your output is getting longer and longer because you never reset it. You want to reset code each time you loop to a new distance. You check to see if the character is over 127, but you only do that at the end of the loop over the phrase, so it's only checking the last character in the phrase. Likewise, you only add to the output (code) at the end of the loop over the phrase. So you are only outputting the last character for each distance.

Also, your correction for the "word" being over 127 is wrong. All you need to do is subtract 95.
Should I use a while loop? I tried to change it a little
phrase = input("Write here the frase to encrypt -> ")
code = ""
n = 0
while True:
    for character in phrase:
        crypt = ord(character)
        n += 1
        if n >= 127:
            break
        if ord(character) > 127:
            crypt_1 = crypt - 95
    crypt_1 = crypt + n
    code = chr(crypt_1)
print("Your encrypted code could be one of the following: ", code, "\n")
Reply
#4
Did I say there was anything wrong with the for loops? No. The for loops are fine. In fact that while loop is going to cause you problems, because you have no way to get out of it. It will just run forever. The for loops are fine, you just need to put the right things in the right loops.

After the last thread, you had a working encrypter, right? Take that code, all of it, without changing it, and put it in a for loop. Then take out the part that asks for a distance, and make the distance the looping variable. Then take out the part that asks for a phrase, and put that at the beginning.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jul-22-2017, 03:13 PM)ichabod801 Wrote: Did I say there was anything wrong with the for loops? No. The for loops are fine. In fact that while loop is going to cause you problems, because you have no way to get out of it. It will just run forever. The for loops are fine, you just need to put the right things in the right loops.

After the last thread, you had a working encrypter, right? Take that code, all of it, without changing it, and put it in a for loop. Then take out the part that asks for a distance, and make the distance the looping variable. Then take out the part that asks for a phrase, and put that at the beginning.
Thanks! I guess it's going to take some time to get used to writing a script naturally haha, I still do make a lot of mistakes messing the logic of the scripts up.
Reply
#6
You might want to learn functions (there is a link to a tutorial on them in my signature). That will get you used to the ideas of blocks of code and manipulating one block of code with another. You could write an encrypt function that takes a phrase and distance in and returns an encrypted phrase. Then you create a for loop where the body of the function is just the function call and printing the output.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with a simple script Niko047 2 3,328 Jul-21-2017, 09:02 PM
Last Post: Niko047

Forum Jump:

User Panel Messages

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