一起添加2个字母值?

时间:2014-12-04 20:30:15

标签: python

我目前正在尝试使用关键字进行加密。我已经获取了用户输入和关键字输入,并获得了字母表中每个字母的值。 (a = 1,b = 2,c = 3等)我现在需要将这两个值加在一起。由于我在代码中使用了while循环来获取每个字母并取值,因此我无法获取每个单独的值并添加它。有人能给我一个关于如何添加每个值的正确方向的观点吗? 感谢。

def keyEnc():
    alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    gcselist = ['g','c','s','e']
    code = input("Please enter the word you would like to encrypt: ")
    print("User's word is " + code)
    print("The number of letters in the code is: ")
    print(len(code))
    x=0
    while x < len(code):
        currLetterA=code[x]
        print("Letter: ",currLetterA)
        myii=alpha.index(currLetterA)
        myii=myii+1
        print("The Value Is: ",myii)
        x=x+1
#############################################################################################
    key = input("Please enter the keyword you would like to encrypt your word by: ")
    x=0
    while x < len(key):
        currLetter=key[x]
        print("Letter: ",currLetter)
        myi=alpha.index(currLetter)
        myi=myi+1
        print("The Value Is: ",myi)
        finWord = myi+myii
        print(finWord)
        x=x+1
keyEnc()

1 个答案:

答案 0 :(得分:0)

听起来你正在尝试这样做:

cleartext:  somewords = 19 15 13  5 23 15 18  4 19
key:        something = 19 15 13  5 20  8  9 14  7
                       ++++++++++++++++++++++++++++
ciphertext:             38 30 26 10 43 23 27 18 26

是吗?在这种情况下,您需要一个数字列表,而不仅仅是一个输出。另请注意,如果您的明文和密钥长度不完全相同,则无法正常工作。想象:

cleartext:  somewords = 19 15 13  5 23 15 18  4 19
key:        banana    =  2  1 14  1 14  1
                       ++++++++++++++++++++++++++++
ciphertext:             21 16 27  6 37 16  ?  ?  ?

cleartext:  somewords     = 19 15 13  5 23 15 18  4 19
key:        bananahammock =  2  1 14  1 14  1  8  1 13 13 15  3 11
                           ++++++++++++++++++++++++++++++++++++++++
ciphertext:                 21 16 27  6 37 16 36  5 32  ?  ?  ?  ?

然而,在这种情况下,您似乎想要这样:

def key_enc():
    cleartext_word = input("Enter the word you want to encrypt: ")
    key = input("Enter the word you want as a key: ")
    if len(cleartext_word) != len(key):
        # I'm not sure how to handle this, so that's your design decision
        # but possibly just:
        raise ValueError("Clear text and key must have equal length")
    result = list()
    def lookup_letter(letter):
        """Helper function to return a proper index.

        a = 1, b = 2, c = 3, ..."""

        return ord(letter) - 96

    for letter, key_letter in zip(cleartext_word, key):
        result.append(lookup_letter(letter) + lookup_letter(key_letter))

    return result

在这里使用zip是您失踪的关键。您在技术上不需要这样做,您可以像在代码中一样使用while循环:

def key_enc():
    # prompt for the cleartext_word and key as above
    result = []
    i = 0
    while i < len(cleartext_word): # which MUST BE EQUAL TO len(key)
        clear_letter = cleartext_word[i]
        key_letter = key[i]
        result_letter = ord(clear_letter)-96 + ord(key_letter)-96
        result.append(result_letter)

但是这样阅读起来比较困难。

说到难以阅读,基本上你的整个功能是:

result = list(map(lambda letters: sum(map(ord,letters))-192, zip(cleartext_word, key_word)))
# YUCK!!!