Back to Freecodecamp

Step 4

curriculum/challenges/english/blocks/workshop-salary-tracker/68c27eb292bfec6b9402d58f.md

latest1.4 KB
Original Source

--description--

Add a __str__ method to the Employee class. Make it return an f-string with the format name: level, replacing name and level with the corresponding attributes.

After that, print charlie_brown to the console.

--hints--

Your Employee class should have a __str__ method with a self parameter.

js
({ test: () => assert(runPython(`_Node(_code).find_class("Employee").find_function("__str__").has_args("self")`)) })

Your __str__ method should return an f-string with the format name: level, replacing name and level with the values of self._name and self._level, respectively.

js
({ test: () => runPython(`
    import io
    import sys
        
    captured_output = io.StringIO()
    sys.stdout = captured_output
        
    empl = Employee("Frank", "dreamer")
    print(empl)
        
    sys.stdout = sys.__stdout__
    output = captured_output.getvalue()
        
    assert "Frank: dreamer" in output
`) })

You should print charlie_brown to the console.

js
({ test: () => assert(runPython(`_Node(_code).has_stmt("print(charlie_brown)")`)) })

--seed--

--seed-contents--

py
--fcc-editable-region--
class Employee:
    def __init__(self, name, level):
        self._name = name
        self._level = level

    

charlie_brown = Employee('Charlie Brown', 'trainee')

--fcc-editable-region--