Chris B.
Chris B.

Reputation: 59

Converting scrambled alphabet

I need to take an input of a scrambled alphabet and covert it to the A-Z alphabet.

I think I need to change these to integers.

Any idea how to take a scrambled input and changed it to integers?

UPDATE:

Here is the code I've written so far. I can't use the built in functions posted, must be things we've learned already.

If the user input is:

VNKW KW BO 1WV WJHFJV BJWWXEJ!

the desired output is:

THIS IS MY 1ST SECRET MESSAGE

import random

def main():
    encrypt = [" "] * 26   #all letters available

    for numbah in range(26):
        letter = chr(numbah+65)
        print (letter, end="")
        # find position for number
        notfound = True
        while notfound:
            position = random.randint(0, 25)
            if encrypt[position] == " ":
                notfound = False
        encrypt[position] = letter

    print("\nScrambled: ", end="")
    for numbah in range(26):
        print(encrypt[numbah], end="")
    print("\n\n ")

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

    print("Now input the scrambled message:  " + msg)
    print("Your unscrambled message reads: ", end="")
    for alpha in msg.upper():
        if alpha < "A" or alpha > "Z":
            print(alpha,end="")
        else:
            print(encrypt[ ord(alpha) - 65 ], end="")

main()

Upvotes: 2

Views: 495

Answers (2)

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

In order to get the ASCII number of a specific character, you can use ord().

print ord("a") => 97

You can then manipulate this value, and convert back to an ASCII character using chr().

print chr(98) => "b"

This should give you a good head start. You can view all the ASCII character numbers here.

Upvotes: 1

Sven Marnach
Sven Marnach

Reputation: 602145

Since this is a homework question, I only hint you at the functions you could use to easily implement this: Take a look at string.maketrans() and str.translate().

Upvotes: 8

Related Questions