简体   繁体   中英

How do I assign each letter in a string a value then add those values to another string?"

def code_maker():

    values = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7, "i":8, "j":9, "k":10, "l":11, "m":12, "n":13, "o":14, "p":15, "q":16, "r":17, "s":18, "t":19, "u":20, "v":21, "w":22, "x":23, "y":24, "z":25}

    key = input("Please enter a four letter key ").lower()
    code = input("Please enter a string ").lower()

    added_code = (key[0] + code[0]) + (key[1] + code[1]) + (key[2] + code[2]) + (key[3] + code[3]) 

I'm trying to do a Caesar cipher type of thing where I assign each letter in a string to a corresponding number then adding its value to another letter in another string but I have no idea how to go about finding each letters associated values.

Don't you want to use already written code? From here you can find the following:

 # Caesar Cipher



MAX_KEY_SIZE = 26



def getMode():
    while True:
         print('Do you wish to encrypt or decrypt a message?')

         mode = input().lower()

         if mode in 'encrypt e decrypt d'.split():

             return mode

         else:

             print('Enter either "encrypt" or "e" or "decrypt" or "d".')



def getMessage():

    print('Enter your message:')

    return input()



def getKey():

    key = 0

    while True:

        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))

        key = int(input())

        if (key >= 1 and key <= MAX_KEY_SIZE):

            return key



def getTranslatedMessage(mode, message, key):

    if mode[0] == 'd':

        key = -key

    translated = ''



    for symbol in message:

        if symbol.isalpha():

            num = ord(symbol)

            num += key



            if symbol.isupper():

                if num > ord('Z'):

                    num -= 26

                elif num < ord('A'):

                    num += 26

            elif symbol.islower():

                if num > ord('z'):

                    num -= 26

                elif num < ord('a'):

                    num += 26



            translated += chr(num)

        else:

            translated += symbol

    return translated



mode = getMode()

message = getMessage()

key = getKey()



print('Your translated text is:')

print(getTranslatedMessage(mode, message, key))

It will ask if you want to decrypt or encrypt message. Say e if want to encrypt. Then it asks for key number. Than for message, and it outputs the encrypted version of your message.

Try zip().

def code_maker():
    values = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7, "i":8, "j":9, "k":10, "l":11, "m":12, "n":13, "o":14, "p":15, "q":16, "r":17, "s":18, "t":19, "u":20, "v":21, "w":22, "x":23, "y":24, "z":25}

    key = input("Please enter a four letter key ").lower()
    code = input("Please enter a string ").lower()
    print(list(zip(key,code)))

Output:

Please enter a four letter key abcd
Please enter a string wxyz
[('a', 'w'), ('b', 'x'), ('c', 'y'), ('d', 'z')]

Please let me know if you need the output in a different form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM