python - Python 中的凯撒密码求解器
问题描述
我在 python 中做了一个凯撒密码求解器,但是没有错误,它只是没有打印任何东西。我做过类似的事情,但以前没有发生过。
with open("C:\\Users\\Rajive\\AppData\\Local\\Programs\\Python\\Python3.4.3\\brit-a-z.txt", 'r') as f:
check_list = [x.strip() for x in f.readlines()]
def crackcaesar(encrypted):
for i in range(26):
totalwords = 0
numright = 0
answer = []
if i != 0:
for symbol in encrypted:
if symbol.isalpha():
neword = (ord(symbol) + i)
if symbol.isupper():
if neword > ord('Z'):
neword -= 26
elif neword < ord('A'):
neword += 26
elif symbol.islower():
if neword > ord('z'):
neword -= 26
elif neword < ord('a'):
neword += 26
newletter = chr(neword)
answer.append(newletter)
for word in str(answer):
totalwords += 1
for line in check_list:
if line == word:
numright += 1
if (numright // 2) > (totalwords // 2):
print(str(answer))
print("Type your encoded caesar cipher message")
while True:
crackcaesar(input())
解决方案
问题是numright
永远不会大于totalwords
。尝试
if numright == totalwords:
print(str(answer))
另外,answer
是一个字符列表。 str(answer)
会给你"['a', 'b', 'c']"
。你需要使用"".join(answer)
.
推荐阅读
- javascript - NodeJS 从 DB 1 中检索记录 1 与全部
- python-3.x - 将字典列表的字符串表示形式转换为字典列表
- angular - Angular 通过输入中的 id 从数组中获取对象
- android - Android 模拟器没有与我的 React Native 应用程序一起运行
- postgresql - Odoo 12 数据库连接超时与繁重的 cron 作业
- eclipse - 如何将源文件链接到基于 Eclipse 的 IDE
- linux - 通过 ssh 连接到 jupyter notebook 服务器的问题
- pycharm - Python 社区:我们可以在社区版本中使用 BDD 框架吗?
- ios - 用新通知替换旧的 LOCAL 通知
- python - 是否有与 Python 的 xarray 等效的 Matlab?