1

我在文件上尝试了以下程序,但没有得到准确的结果。解码的文件不是原始消息的精确副本。有些字母在某处被吃掉了。

""" 这个 python 脚本文件通过以下方式加密消息来工作:

不要替换偶数处的字符。将奇数位置的字符替换为它们的位置编号。如果它们超过“z”,那么它们将再次从“a”开始。

例如:对于消息“hello”将是“ieolt”,对于消息“maya”将是“naba”

import os
import time
import sys

def openfile(filename):    # opens file with name 'filename'
    file_to_open = open(filename,'a+')
    return file_to_open

def readfile(filename):    # returns a long string with the info of the message in 'filename' file.
    time.sleep(0.3)
    print "Reading from the file "+filename
    reading_file = openfile(filename)
    read_msg = reading_file.read()
    return read_msg

def decode(msg):            # returns decoded message of input message 'msg'.
 """  reverse function of encode(msg)  """
    decoded_message = ""
    letters = " abcdefghijklmnopqrstuvwxyz"
    time.sleep(0.5)
    print " Encoding ...."
    print "encoding the message...."
    index_of_msg = 0
    for char in msg.lower():
        if char.isalpha():
            if index_of_msg%2 == 0 :                
                decoded_message += letters[(letters.rfind   (char)- (index_of_msg+1))%26]  # changed msg.rfind(char) to index_of_msg
            else:
                decoded_message += char
        else:
            decoded_message += char
        index_of_msg +=1
    time.sleep(0.5)
    print "decoding completed"
    return decoded_message

def encode(msg):            # returns encoded message of input message 'msg'.
    """Clean up work must be done here.."""

    encoded_message = ""
    letters = " abcdefghijklmnopqrstuvwxyz"
    time.sleep(0.5)
    print " Encoding ...."
    print "encoding the message...."
    index_of_msg = 0
    for char in msg.lower():
        if char.isalpha():
            if index_of_msg%2 == 0 :          
                encoded_message += letters[(letters.rfind(char)+ (index_of_msg+1))%26]  # changed msg.rfind(char) to index_of_msg
            else:
                encoded_message += char
        else:
            encoded_message += char
        index_of_msg +=1

    time.sleep(0.5)
    print "encoding completed"
    return encoded_message

def write(msg,filename):   # writes the message 'msg' given to  it, to the file named 'filename'.
    print "Opening the file "+filename
    time.sleep(0.4)
    file_output = openfile(filename)
    print filename + " opened and ready to be written"
    time.sleep(0.3)
    print "Writing the encoded message to the file "+filename
    file_output.write(msg)
    file_output.close()
    time.sleep(0.4)
    print "Writing to the file has completed."


def start():               # Starter main function that     incorporates all other functions :)

    os.chdir('aaest/')
    clear = lambda: os.system('clear')
    clear()
    print "Hi, Welcome to this Encryption Program. \n"
    filename = raw_input("Enter the file name in which you  stored the message: ")
    print "Opening the file " + filename
    time.sleep(0.5)
    openfile(filename)
    print filename +" opened up and ready, retrieving the   message from it."
    time.sleep(0.5)
    message = readfile(filename)
    print "The message of the "+filename+" is retrieved."
    time.sleep(0.5)
    encoded_msg = encode(message)
    time.sleep(0.3)
    decoded_msg = decode(encoded_msg)
    encoded_file = raw_input("Enter the name of the output file     in which encoded message will be saved :")
    write(encoded_msg,encoded_file)
    decoded_file = raw_input("Enter the name of the output file     in which decoded message will be saved :")
    write(decoded_msg,decoded_file)


start()

谁能帮我解决这个问题。

4

1 回答 1

2

您的部分问题是您的letters字符串以空格而不是“a”开头。所以如果你有一个'y'作为字符串的第一个字符,它会被一个空格替换。然后,当您尝试解码时,该空间无法通过您的isalpha检查并且不会被替换。

这段代码可以通过多种方式变得更简洁,但这是我看到的第一个逻辑错误。除非我遗漏了什么,否则letters = "abcdefghijklmnopqrstuvwxyz"应该修复那个特定的错误。或者更好的是,使用string.ascii_lowercase.

于 2013-08-12T00:27:55.820 回答