【问题标题】:Python Caesar Cipher Using Turkish Alphabet Array使用土耳其字母数组的 Python 凯撒密码
【发布时间】:2022-07-02 19:53:40
【问题描述】:

第一个 for 循环(for i in range )无法正常工作,即使第二个循环正常工作。

您可以在下面找到我遇到问题的代码和循环:

alpLower = ["a","b","c","ç","d","e","f","g","ğ","h","ı","i","j","k","l","m","n","o","ö","p","r","s","ş","t","u","ü","v","y","z"]
alpUpper = ["A","B","C","Ç","D","E","F","G","Ğ","H","I","İ","J","K","L","M","N","O","Ö","P","R","S","Ş","T","U","Ü","V","Y","Z"]
encrypt=[]
b=0

text = input("Please enter a text:\t")
shift = int(input("Please enter the shift value:\t"))

for c in text:

#problem occurs at the loop below
for i in range(0,29,1):
    if c == alpLower[i]:
        b+=1
        c = alpLower[(i+shift)%29]
        encrypt.append(c)
        break
    else:
        b=0
#problem occurs at the loop above

for i in range(0,29,1):
    if c == alpUpper[i]:
        b+=1
        c = alpUpper[(i+shift)%29]
        encrypt.append(c)
        break

    else:
        b=0

if b==0:
    encrypt.append(c)

print("\nEncrpyted text:")

for i in encrypt:
    print(i,end="")

【问题讨论】:

  • 你能修复一下缩进吗?
  • 你能提供一个预期输出的例子吗?你有什么,或者错误信息?
  • 如果 b == 0,即不在字母表中,字符是否应该不加密?

标签: python cryptography caesar-cipher


【解决方案1】:

我会重新组织你的代码,让它看起来更像这样:

def ceasercypher(steps, word):
        alpLower,  alpUpper = ["a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p", "r",
                    "s", "ş", "t", "u", "ü", "v", "y", "z"], ["A", "B", "C", "Ç", "D", "E", "F", "G", "Ğ", "H", "I", "İ", "J", "K", "L", "M", "N", "O", "Ö", "P", "R",
                    "S", "Ş", "T", "U", "Ü", "V", "Y", "Z"]
        answer = []
        for letter in word:
            if letter.isupper():
                answer.append(alpUpper[int((steps + alpUpper.index(letter))%len(alpUpper))])
            if letter.islower():
                answer.append(alpLower[int((steps + alpLower.index(letter))%len(alpLower))])
        return "".join(answer)
print(ceasercypher(0, "ağV"))

只是为了好玩,您可以随时尝试将其写短! 但是,如果您在团队中工作,或者希望以后能够理解它,这是不好的做法。 我叫它“程序员没用的flex,很多bug的根源”

def ceasercypher(steps, word):
    alpLower,  alpUpper = ["a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p", "r",
                "s", "ş", "t", "u", "ü", "v", "y", "z"], ["A", "B", "C", "Ç", "D", "E", "F", "G", "Ğ", "H", "I", "İ", "J", "K", "L", "M", "N", "O", "Ö", "P", "R",
                "S", "Ş", "T", "U", "Ü", "V", "Y", "Z"]
    return "".join([alpUpper[int((steps + alpUpper.index(letter))%len(alpUpper))] if letter.isupper() else alpLower[int((steps + alpLower.index(letter))%len(alpLower))] if letter.islower() else " [INVALIDCHAR] " for letter in word])
print(ceasercypher(1, "ağVZ%K%"))

【讨论】:

    【解决方案2】:

    实际上,你不需要遍历列表中的每个元素来知道它的索引是什么,你可以使用 index() 来获取元素的索引。

    animals = ['cat', 'dog', 'giraffe', 'elephant', 'monkey']
      
    # Will print the index of 'giraffe' in animals
    print(animals.index('giraffe'))
    

    输出:

    将其应用于您的代码:

    for c in text:
        if c in alpUpper:
            i = alpUpper.index(c)
            c = alpUpper[(i + shift) % 29]
            b += 1
            encrypt.append(c)
        else:
            b = 0
    

    【讨论】:

    • 这也是一个很好的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2013-03-13
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多