8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

MIT 问题集 4c:类型错误:加密消息不可下标

M.M. CAN 2月前

19 0

def decrypt_message_try_pads(ciphertext, pads): ''' 给定一个字符串密文和用于创建它的可能垫片列表,找到用于创建密文的垫片我们将考虑......

def decrypt_message_try_pads(ciphertext, pads):
    '''
    Given a string ciphertext and a list of possible pads
    used to create it find the pad used to create the ciphertext

    We will consider the pad used to create it the pad which
    when used to decrypt ciphertext results in a plaintext
    with the most valid English words. In the event of ties return
    the last pad that results in the maximum number of valid English words.

    ciphertext (EncryptedMessage): The ciphertext
    pads (list of lists of ints): A list of pads which might have been used
        to encrypt the ciphertext

    Returns: (PlaintextMessage) A message with the decrypted ciphertext and the best pad
    '''
    word_list = load_words('words.txt')

    max_word_count = 0
    best_pad = None
    best_decrypted_message = ""

    for pad in pads:
        # Debug: show current pad being tried
        print(f"Trying pad: {pad}")

        # Decrypt the message using the current pad
        encrypted_message = ps4b.EncryptedMessage(ciphertext)
        decrypted_message_object = encrypted_message.decrypt_message(pad)
        print(type(decrypted_message_object))
        decrypted_message = decrypted_message_object.get_text()

        # Split the decrypted message into words
        decrypted_message_list = decrypted_message.split()

        # Count the number of valid English words
        word_count = sum(is_word(word_list, word) for word in decrypted_message_list)

        # Debug: Show the decrypted message and word count


        # Update best pad and message if this pad produces more valid words
        if word_count > max_word_count:
            max_word_count = word_count
            best_pad = pad
            best_decrypted_message = decrypted_message
        elif word_count == max_word_count:
            best_pad = pad
            best_decrypted_message = decrypted_message

    # Debug: Final selection

    return ps4b.PlaintextMessage(best_decrypted_message, best_pad)
Traceback (most recent call last):
  File "C:\Users\Asus\PycharmProjects\pythonProject1\1_ps4\test_ps4bc_student.py", line 175, in test_try_pads
    result_plaintextMessage = studentc.decrypt_message_try_pads(enc_msg, pads)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Asus\PycharmProjects\pythonProject1\1_ps4\ps4c.py", line 95, in decrypt_message_try_pads
    decrypted_message_object = encrypted_message.decrypt_message(pad)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Asus\PycharmProjects\pythonProject1\1_ps4\ps4b.py", line 185, in decrypt_message
    decrypted_text = self.apply_pad(inverted_pad)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Asus\PycharmProjects\pythonProject1\1_ps4\ps4b.py", line 66, in apply_pad
    string += self.shift_char(self.msg_text[i], pad[i])
                              ~~~~~~~~~~~~~^^^
TypeError: 'EncryptedMessage' object is not subscriptable


Process finished with exit code 1
# Codes for the Message Object
class Message(object):
    def __init__(self, input_text):
        '''
        Initializes a Message object

        input_text (string): the message's text

        a Message object has one attribute:
            the message text
        '''
        self.msg_text = input_text

    def __repr__(self):
        '''
        Returns a human readable representation of the object
        DO NOT CHANGE

        Returns: (string) A representation of the object
        '''
        return f'''Message('{self.get_text()}')'''

    def get_text(self):
        '''
        Used to access the message text outside of the class

        Returns: (string) the message text
        '''
        return self.msg_text

    def shift_char(self, char, shift):
        '''
        Used to shift a character as described in the pset handout

        char (string): the single character to shift.
                    ASCII value in the range: 32<=ord(char)<=126
        shift (int): the amount to shift char by

        Returns: (string) the shifted character with ASCII value in the range [32, 126]
        '''
        # Get the ASCII value of the character
        char_ascii_num = ord(char)

        shifted_ascii_num = (char_ascii_num + shift -32) % 95 + 32
        return chr(shifted_ascii_num)
    def apply_pad(self, pad):
        '''
        Used to calculate the ciphertext produced by applying a one time pad to the message text.
        For each character in the text at index i shift that character by
            the amount specified by pad[i]

        pad (list of ints): a list of integers used to encrypt the message text
                        len(pad) == len(the message text)

        Returns: (string) The ciphertext produced using the one time pad
        '''

        string = ""
        for i in range(len(pad)):
            string += self.shift_char(self.msg_text[i], pad[i])

        return string
#Codes for the EncryptedMessage Object
class EncryptedMessage(Message):
    def __init__(self, input_text):
        '''
        Initializes an EncryptedMessage object

        input_text (string): the ciphertext of the message

        an EncryptedMessage object inherits from Message. It has one attribute:
            the message text (ciphertext)
        '''
        super().__init__(input_text)


    def __repr__(self):
        '''
        Returns a human readable representation of the object
        DO NOT CHANGE

        Returns: (string) A representation of the object
        '''
        return f'''EncryptedMessage('{self.get_text()}')'''

    def decrypt_message(self, pad):
        '''
        Decrypts the message text that was encrypted with pad as described in the writeup

        pad (list of ints): the new one time pad used to encrypt the message.
            len(pad) == len(the message text)

        Returns: (PlaintextMessage) the decrypted message (containing the pad)
        '''
        # We need to invert the pad to decrypt the message
        inverted_pad = [-shift for shift in pad]

        # Apply the inverted pad to decrypt the message
        decrypted_text = self.apply_pad(inverted_pad)

        # Return a PlaintextMessage object with the decrypted text and the original pad
        return PlaintextMessage(decrypted_text, pad)

大家好,我目前正在研究 MIT Pset 4,我通过了所有的测试,但有一个测试失败了,那就是 test_try_pads。错误消息是 \'TypeError: 'EncryptedMessage' 对象不可下标'。在搜索了很多 chatgpt 东西和谷歌之后,我不知道如何解决这个错误。如果你们能帮助我,我将不胜感激。错误出在行 decrypted_message_object = encrypted_message.decrypt_message(pad) 。因为在我的代码的实现中,运行 decrypt_message 方法后,它将返回一个新类,如果你们想要更多信息我可以分享,因为它有点难以描述。这是因为该类是从其他文件导入的,如果我在原始文件上测试它,它可以工作,但我不确定为什么导入后它无法运行 decrypt_message 属性。

帖子版权声明 1、本帖标题:MIT 问题集 4c:类型错误:加密消息不可下标
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由M.M. CAN在本站《python》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 不,这不是完整的回溯。它以“Traceback”开头,以错误消息结尾,通常有十几行。

  • @tripleee 他们在您发表评论前将近一个小时就添加了它。也许您没有看到它,因为他们将其放在与代码相同的代码块中,但我已经修复了它。

返回
作者最近主题: