Make an object print the way you want.
What to build
A class with a __str__ method.
Requirements
- Complete
__str__so that a point prints as(x, y). - Leave the rest of the program as it is.
Input
One line with two whole numbers.
Output
One line.
Constraints
Each number is from -1000 to 1000.
Example
Input:
3 4
Output:
(3, 4)
Why it works
print asks the object for its text through __str__, so the class decides how it appears everywhere.
Examples
Example 1 · Input
3 4
Expected output
(3, 4)
Example 2 · Input
-1 0
Expected output
(-1, 0)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
__str__ must return text, not print it.
Hint 2
An f-string makes the format easy.
Solution
Show one correct solution (try it yourself first)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
x, y = [int(v) for v in input().split()]
print(Point(x, y))