Object-oriented programming · 12 / 26 Exercise Medium

Person class and method ToString()

Objective

Create a Python program that asks the user for three names and stores them in a list of objects of the Person class.

Instructions

  • Create a class called Person with an attribute called name.
  • Define the special method __str__ in the Person class to customize how a person is displayed as text: Hello! My name is followed by the name.
  • In the main program, ask the user for three names and store them in a list of Person objects.
  • Display each person on the screen (print uses the __str__ method).
Input
Juan
Sara
Carlos
Output
Hello! My name is Juan
Hello! My name is Sara
Hello! My name is Carlos

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise