python 凯撒密码项目

问题描述 投票:0回答:1

这是我的凯撒密码的代码,其工作原理是将第一个数字设置为所需的移位,然后将第二个字母设置为移位+1,依此类推。但是当我输入

gfkzia te rt rdeymabprtf ninflz
进行解密时,shift=4 我期望
caesar is de allereerste keizer
但我得到
Caesar is de allereerste kei@er
我也得到全部大写,因为我使用了
.upper
。我怎样才能得到什么信?例如,如果我输入大写字母,我会得到大写字母,如果输入小写字母,我会得到小写字母。

# try to make a simple interface with while loops
message=input("geef jouw tekst in: ")
encrpyt=input("als je wil enycprpen geef e in en als je wil dycrpten druk d in: ")
while True:
  if encrpyt.lower() == "e" or encrpyt.lower() == "d":
    break
  elif encrpyt.lower() != "e" or encrpyt.lower() != "d":
    print("Sorry, maar ik begrijp het niet. Kunt u op e of d drukken om verder te gaan?")
    encrpyt = input("als je wil enycprpen geef e in en als je wil dycrpten druk d in: ")

shift= int(input("met hoeveel numbers wil je shifften: "))

# values that will be used later on
result=""
last_cahr_code = 90
first_chr_code = 65
total_letter=26
domminoes_effect= 0

# try to give every variable a proper name to avoid confuison 

for charachters in message.upper():
  if encrpyt.lower() == "e":
   if charachters.isalpha():
     unicode = ord(charachters)
     new_unicode = unicode + (shift + domminoes_effect)
     domminoes_effect += 1
 
     if new_unicode > last_cahr_code:
        new_unicode -= total_letter

     new_character = chr(new_unicode)
     result += new_character
 
  else:
     domminoes_effect += 1
     result += charachters
  if encrpyt.lower() == "d":
     if charachters.isalpha():
       unicode = ord(charachters)
       new_unicode = unicode -  shift - domminoes_effect
       domminoes_effect +=1

       if new_unicode < first_chr_code:
        new_unicode += total_letter

       new_character = chr(new_unicode)
       result += new_character
     else:
       domminoes_effect +=1
       result += charachters
print(f"resultaat: {result}")
python loops uppercase lowercase
1个回答
0
投票

请勿将消息转换为大写。相反,请专门检查大写和小写范围,以便您可以将加密值保持在同一范围内。

另一个问题是,当您使用多米诺骨牌效应时,调整将高于 26,因此当超出字母范围时仅添加或减去 26 是不够的。当你增加这个值时,当它达到 26 时,你应该回绕到 0。

尽量避免重复代码。在下面的版本中,我将附加更新字符并递增

domminoes_effect
的代码移出
if/else
块,因为每个字符都是相同的。

# try to make a simple interface with while loops
message=input("geef jouw tekst in: ")
while True:
    encrpyt=input("als je wil enycprpen geef e in en als je wil dycrpten druk d in: ").lower()
    if encrpyt == "e" or encrpyt == "d":
        break
    print("Sorry, maar ik begrijp het niet. Kunt u op e of d drukken om verder te gaan?")
    encrpyt = input("als je wil enycprpen geef e in en als je wil dycrpten druk d in: ")

shift= int(input("met hoeveel numbers wil je shifften: "))

# values that will be used later on
result=""
last_upper = ord('Z')
first_upper = ord('A')
last_lower = ord('z')
first_lower = ord('a')
total_letter=26
domminoes_effect= 0

# try to give every variable a proper name to avoid confuison

for charachters in message:
    if encrpyt == "e":
        if charachters.isalpha():
            unicode = ord(charachters)
            new_unicode = unicode + (shift + domminoes_effect)

            if (charachters.isupper() and new_unicode > last_upper) or (charachters.islower() and new_unicode > last_lower):
                new_unicode -= total_letter

            charachters = chr(new_unicode)
    else:
        if charachters.isalpha():
            unicode = ord(charachters)
            new_unicode = unicode -  shift - domminoes_effect

            if (charachters.isupper() and new_unicode < first_upper) or (charachters.islower() and new_unicode < first_lower):
                new_unicode += total_letter

            charachters = chr(new_unicode)

    result += charachters
    domminoes_effect = (domminoes_effect + 1) % 26

print(f"resultaat: {result}")
© www.soinside.com 2019 - 2024. All rights reserved.