Jul-22-2017, 02:58 PM
(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).Should I use a while loop? I tried to change it a little
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.
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")