python - Python TypeError: list indices must be integers or slices, not tuple. Caesar Cypher Decoder
问题描述
I am working on writing a decrypter for Caesar Cyphers that won't require the user to input the amount the message was changed by. It will then look for common letter occurrences and print the ones with the most matches down to the lest matches. It will print them all fine. Just without an order. My aim was to save the decoded messages and the amount of occurrences to a 2d array. Yet I have reached a problem with this. It returns this error when I try to edit the array. Thanks in advance for any help.
import sys
import time
from time import sleep as s
import random
import collections
global c1, message, letters, array, i, output
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lletters = letters.lower()
length = len(letters)
space = " "
length1 = len(space)
c1 = int(0)
check = ["hi", "ing", "ai", " i ", "oo", "e ", "as", "wh", "er", "th", "re", "or", "eir", " if ", "en", "ph", "zz", "ll", "ff", "ei", "ie", " a ", "qu", "eu", "au", "ue", "ca"]
output = [25,1]
message = input("Enter the thing you want to decrypt. ")
def decrypt():
global c1, letters, message, list1
decrypted = ''
for counter in range(1,26):
decrypted = ""
c1 = int(counter)
p = int(counter - int(1))
for chars in message:
if chars in letters:
num = letters.find(chars)
num -= c1
num = num%length
decrypted += letters[num]
elif chars in lletters:
num = lletters.find(chars)
num -= c1
num = num%length
decrypted += lletters[num]
elif chars in space:
decrypted += space
ho = int(0)
h1 = int(0)
for i in range(len(check)):
h = check[i]
if h in decrypted.lower():
output[p,1] = int(h1)
h1 = h1 + int(1)
output[p,1] = int(h1)
if ho == int(0):
ho = int(1)
print(str(output[p,2]))
print(decrypted)
decrypt()
解决方案
您的错误在于列表切片语法。它是 l[start:finish] 而不是 l[start,finish]。改变这一行:
print(str(output[p,2])
至:
print(str(output[p:2])
推荐阅读
- python - 如何更改特定索引处按钮的位置
- ios - MCAdvertiserAssistant 不会在连接请求时提醒主机
- rest - 命名 RESTful API 路由。对资源的操作的命名。方法是什么?
- snowflake-cloud-data-platform - 如何获取雪花主机和端口号以在 SAP Analytics Cloud 中创建连接?
- python - 我的模拟大部分时间都会出错
- node.js - 将 yargs 转换为 process.argv
- python-3.x - 具有多个标志和参数的 argparse
- prolog - Prolog:关于列表和递归通缉的代码解释
- c# - Project X 以框架“.NETStandard”为目标。实体框架包管理器控制台工具不支持此框架
- hyperledger-fabric - 在同一组织的多个对等点上使用外部链码?