为什么定义中的代码在 VSCode 上不起作用

问题描述 投票:0回答:1

下面我有一个作业代码。通常我使用 Pycharm,但我听说过很多关于 VSCode 的好消息,所以我想尝试一下。问题是我的代码可以运行,但如果在定义中编码了某些内容,则不会提供任何输出。如果我将 print 放在定义之外的类之前或之下,它确实有效。这是有原因的吗?
我知道类应该向右缩进一个,但我现在似乎无法在 Stack overflow 中修复它。我只想在我的代码中打印以了解我自己的步骤并确保我了解所有内容,但我似乎无法让它工作。

import numpy as np
import typing

class IntelDevice:
    def __init__(self, width: int, height: int, enc_locations: typing.List[str], enc_codes: typing.List[str], caesar_shift: int):
        """
        The IntelDevice object, containing all information and functions required for encoding and decoding messages,
        processing raw encoded locations, efficiently searching for locations based on codes and returning encoded
        answers.  

        :param width: The width (number of columns) of the 2D distance/location grid (self.loc_grid) that we have to fill in
        :param height: The height (number of rows) of the 2D distance/location grid (self.loc_grid) that we have to fill in
        :param enc_locations: A list of encoded location names that correspond to the locations in self.loc_grid
        :param enc_codes: A list of encoded codes (ints) that have to be entered into self.loc_grid
        :param caesar_shift: The caesar shift constant used to encode messages. You may assume this will always be in the set 
                                                                                                                        {0,1,...,26}. We do NOT use modulo calculations for our caesar cipher. 

        You do not need to change this function
        """
        self.width = width
        self.height = height
        self.enc_locations = enc_locations
        self.enc_codes = enc_codes
        self.caesar_shift = caesar_shift

        self.loc_grid = np.zeros((height, width))
        self.coordinate_to_location = dict()  # maps locations (y,x) to their names

    def encode_message(self, msg: str) -> str:
        """
        A function that encodes a given string using a simplified form of caesar cipher (without using modulo). Every character of the string will be 
        transformed into the ordinal/numerical representation and encoded by shifting the number with self.caesar_shift 
        (through addition). Afterward, the shifted numbers are transformed into bitstring representation.

        For example, suppose we want to encode the message 'hello' with a caesar shift of 5. 
        The corresponding encoded message (output of this function) would be '1101101 1101010 1110001 1110001 1110100'. Note that the 
        number of bitstrings separated by spaces is equal to the number of characters of the string 'hello'. 
        Let's look at the first character 'h'. Its ordinal representation is 104. We shift its representation by 5, giving us 109. 
        109 is then transformed into a bitstring, which gives us 1101101 (the first bitstring in the encoded message). 

        Hints: the following built-in Python functions may be of use
        - ord(x): takes a character x as input and returns the ordinal representation
        - '{0:b}'.format(x): transforms a number x into a bitstring

        :param msg: The input message (string) that should be encoded

        Returns: the encoded message
        """
        # TODO
        for i in msg:
            new_msg = msg.replace(i, '{0:b}'.format(ord(i)))
            print(new_msg)
python unit-testing visual-studio-code output definition
1个回答
0
投票

正如 John Gordon 在评论中所说,您定义了类和方法。但是,您没有使用您定义的方法。

您可以添加一行来运行此方法:

encode_message(self, "str")

如下图所示:

© www.soinside.com 2019 - 2024. All rights reserved.