Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

encryption - 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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your mistake is in the list slicing syntax. It is l[start:finish] not l[start, finish]. change this line:

print(str(output[p,2])

to:

print(str(output[p:2])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...