Question

This is my assignment: Write a program which DECRYPTS a secret message.

It should first prompt the user for the scrambled alphabet. It then should ask for the secret message. Finally, it outputs the unscrambled version.

Note that there are exactly 26 characters input for the scrambled alphabet. All alphabetic characters are translated into their decoded equivalents (which will take a WHILE loop), and all other, non-alphabetic characters should be output exactly as they were without translation.

This is my code so far:

decrypt = ["*"] * 26

scram_alphabet = input("Please input the scrambled alphabet in order: ")

while len(scram_alphabet) != 26:
    scram_alphabet = input("Please input the scrambled alphabet in order. The alphabet must have 26 characters: ")

num = 0

for each_letter in scram_alphabet:
    decrypt[num] = ord(each_letter)
    num = num + 1

print()
print()

msg = input("Now input your scrambled message: ")

print()
print()

num = 0

alphabet = [" "] * 26
for letter in range (26):
    alphabet[letter] = letter + 65

while num < 26:
    alphabet [num] = decrypt [num]
    print(chr(alphabet[num]))
    num = num + 1


for alpha in msg.upper():
    if alpha < "A" or alpha > "Z":
        print(alpha,end="")
    else:
        print(chr(decrypt[ ord(alpha) - 65 ]), end="")

I can't seem to figure out how to descramble the alphabet using a while loop.

Currently if I input 'XQHAJDENKLTCBZGUYFWVMIPSOR' as the alphabet and 'VNKW KW BO 1WV WJHFJV BJWWXEJ!' as the secret message, the program prints out the scrambled alphabet and an "unscrambled" message that reads "IZTP TP QG 1PI PLNDLI QLPPSJL!". The unscrambled message is actually "THIS IS MY 1ST SECRET MESSAGE!"

Any helpers?

Was it helpful?

Solution

You are mapping in the wrong direction. For example, let's take the first letter. The V should become T, but why?

The answer is because the spot that V occupies in the alphabet is the spot that T normally occupies. Instead, you are looking for the spot that V would occupy in the alphabet, and substituting in that letter, which is I.

In short, your current program is a scrambler and not a de-scrambler, and so you're just scrambling the message a second time. For example, if I use the same alphabet but instead supply the original message, the result I get is identical to the scrambled message you have provided:

Please input the scrambled alphabet in order: 'XQHAJDENKLTCBZGUYFWVMIPSOR'
Now input your scrambled message: 'THIS IS MY 1ST SECRET MESSAGE!'
VNKW KW BO 1WV WJHFJV BJWWXEJ!

This is the problem line:

decrypt[num] = ord(each_letter)

Instead, you need to build the map in the opposite direction:

decrypt[ord(each_letter) - 65] = num + 65

Now the program works:

Please input the scrambled alphabet in order: 'XQHAJDENKLTCBZGUYFWVMIPSOR'
Now input your scrambled message: 'VNKW KW BO 1WV WJHFJV BJWWXEJ!'
THIS IS MY 1ST SECRET MESSAGE!

OTHER TIPS

From what I can tell, it looks like your script is running correctly. The unscrambled message doesn't appear to be "THIS IS MY 1ST SECRET MESSAGE!"

Here's some manual translation I did using your alphabet:

Key: XQHAJDENKLTCBZGUYFWVMIPSOR
     ABCDEFGHIJKLMNOPQRSTUVWXYZ

Original:   VNKW KW BO 1WV WJHFJV BJWWXEJ!
Translated: IZTP ...

It looks like you need a different example to test with, or perhaps I'm misunderstanding how the scrambling is being performed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top