Python 凯撒密码只对一个字母而不是整个字符串进行加密 所以我在做一个简单的凯撒密码练习,我不能让它破解整个字符串,只是单独的字母。< /P>
Python 凯撒密码只对一个字母而不是整个字符串进行加密 所以我在做一个简单的凯撒密码练习,我不能让它破解整个字符串,只是单独的字母。< /P>,python,python-3.x,input,caesar-cipher,Python,Python 3.x,Input,Caesar Cipher,symbol\u add是有问题的函数 代码如下: import re alphabet = "abcdefghijklmnopqrstuvwxyz" def cleanIt(clean): global alphabet s = re.sub('[^a-z]+', '?', str(clean)) return s def symbol_add(symbol, key): encryptedMsg = "" for x in symbol:
symbol\u add
是有问题的函数
代码如下:
import re
alphabet = "abcdefghijklmnopqrstuvwxyz"
def cleanIt(clean):
global alphabet
s = re.sub('[^a-z]+', '?', str(clean))
return s
def symbol_add(symbol, key):
encryptedMsg = ""
for x in symbol:
position = alphabet.find(x)
newPosition = (position + key) % 26
newLetter = alphabet[nyPosisjon]
encryptedMsg += nyBokstav
return encryptedMsg
def cipher(data,key):
text = ""
if data in alphabet:
text += symbol_add(symbol=data,key=key)
return text
def main():
try:
msg = (input("Write the message you would like to encrypt\n"))
key = int(input("which key would you like to use?\n"))
cleanIt(clean=msg)
print(cipher(data=msg, key=key))
except ValueError:
print("Write a number!")
main()
我确信解决方案非常简单,仍在学习中
我们将非常感谢您对如何解决此问题的任何帮助
import re
alphabet = "abcdefghijklmnopqrstuvwxyz"
def cleanIt(clean):
global alphabet
s = re.sub('[^a-z]+', '?', str(clean))
return s
def symbol_add(symbol, key):
position = alphabet.find(symbol)
newPosition = (position + key) % 26
newLetter = alphabet[newPosition]
return newLetter
def cipher(data,key):
text = ""
for letter in data:
if letter in alphabet:
text += symbol_add(symbol=letter,key=key)
return text
def main():
try:
msg = (input("Write the message you would like to encrypt\n"))
key = int(input("which key would you like to use?\n"))
# Note: you need to assign msg to be equal to cleanIt(clean=msg).
# Just calling cleanIt(clean=msg) won't work, as strings
# are immutable in Python
msg = cleanIt(clean=msg)
print(cipher(data=msg, key=key))
except ValueError:
print("Write a number!")
main()
主要变化是:
- 循环的
已从
移动到symbol\u add
,以便为每个字符调用cipher
symbol\u add
- 在
:main()
->cleanIt(clean=msg)
;这是因为字符串在Python中是不可变的,这意味着您需要重新分配变量msg=cleanIt(clean=msg)
,以基本上指向新字符串msg
Write the message you would like to encrypt
test
which key would you like to use?
1
uftu
此外,尽量坚持单一的命名约定;您有一个函数跟在
camelCase
(cleanIt
)后面,还有一个函数跟在snake\u case
(symbol\u add
)后面。尝试以相同的方式命名所有函数。(Python中的惯例是对函数使用snake\u case
)如果在输入密码方法时以查找方式填充字典,则可以大大简化密码方法。它包含基于提供的键的映射,并将输入字符映射到密码字符
在dict中查找要比在字符串中查找.index()
快得多
使用dict.get(key[,default])
可以为未知项提供“?”
,因此不需要重新导入
,也不需要预处理
阅读有关dict.get()的内容
:
基于小写映射,向chiffre添加大写映射也很简单:
alphabet = "abcdefghijklmnopqrstuvwxyz"
def cipher(data, key):
# in case you change alphabet
la = len(alphabet)
# get the default lookup
chiffre = { c:alphabet[(i+key)%la] for i,c in enumerate(alphabet) }
# create lookup for upper cases as well
chiffre.update( { c.upper():n.upper() for c,n in chiffre.items() } )
# supply ? for all unknowns, use the knowns where possible and return as string
return ''.join( (chiffre.get(c,"?") for c in data) )
def main():
try:
msg = (input("Write the message you would like to encrypt\n"))
key = int(input("which key would you like to use?\n"))
print(cipher(data=msg, key=key))
except ValueError:
print("Write a number!")
main()
输出:
Write the message you would like to encrypt
Hello World
which key would you like to use?
1
Ifmmp?Xpsme
您的各种功能似乎对它们的操作感到困惑。您需要使每个函数一致地处理整个消息或单个字符谢谢!我知道传统的命名法,我是从另一种语言翻译过来的,目的是让它更容易理解和理解:)