Python Object-Oriented Python Advanced Objects Construction Zone

Alfred Moha
Alfred Moha
3,927 Points

need a help

can someone help on construction task python

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

def from__string(cls, string):
    list = from_string("-")
    paattern = []
    for x in from_string:
        if x == "dash":
            pattern.append("-")
            return cls(pattern)

    def __iter__(self):
        yield from self.pattern

    def __str__(self):
        output = []
        for blip in self:
            if blip == '.':
                output.append('dot')
            else:
                output.append('dash')
        return '-'.join(output)


class S(Letter):
    def __init__(self):
         pattern = ['.', '.', '.']
         super().__init__(pattern)

1 Answer

Steven Parker
Steven Parker
176,604 Points

Here's a few hints:

  • the entire method definition must be indented
  • the instructions say this should be done with "\@classmethod"
  • instead of "from__string", the method name should be "from_string" (just one underscore)
  • this method shouldn't be recursive (should not call itself)
  • you may want to "split" the string argument into a list
  • the word "paatern" should be "pattern" (just one "a")
  • since "list" is a keyword it's not a good choice for a variable name
  • instead of a hyphen ("-"), a "dash" should be represented as an underscore ("_")
  • the method should also convert "dot" into a pattern
  • the "return" should not occur until the loop is finished
Alfred Moha
Alfred Moha
3,927 Points

@classmethod def from_string(cls, dash_dot_string): dash_dot_list = dash_dot_string.split('-') pattern_list = [] for item in dash_dot_list: if item == 'dot': pattern_list.append('.') else: pattern_list.append('_')

    return cls(pattern_list)

cant pass

Steven Parker
Steven Parker
176,604 Points

I'd guess you might have one or more indentation errors, since the code looks good otherwise but there's no way to tell if the indentation is correct without markdown formatting.

If you don't find a spacing issue, add formatting to the above to allow it to be checked.