Advertisement
SimeonTs

SUPyF2 Objects/Classes-Exericse - 04. Town

Oct 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. """
  2. Objects and Classes - Exericse
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1734#3
  4.  
  5. SUPyF2 Objects/Classes-Exericse - 04. Town
  6.  
  7. Problem:
  8. Create a class Town. The __init__ method should receive the name of the town. It should also have 3 more methods:
  9. • set_latitude(latitude) - set an attribute called latitude to the given one
  10. • set_longitude(longitude) - set an attribute called longitude to the given one
  11. • __repr__ - return representation of the object in the following string format:
  12. "Town: {name} | Latitude: {latitude} | Longitude: {longitude}"
  13.  
  14. Example:
  15. Test Code:
  16. town = Town("Sofia")
  17. town.set_latitude("42° 41\' 51.04\" N")
  18. town.set_longitude("23° 19\' 26.94\" E")
  19. print(town)
  20.  
  21. Output:
  22. Town: Sofia | Latitude: 42° 41' 51.04" N | Longitude: 23° 19' 26.94" E
  23. """
  24.  
  25.  
  26. class Town:
  27.     def __init__(self, name):
  28.         self.name = name
  29.         self.latitude = ""
  30.         self.longitude = ""
  31.  
  32.     def set_latitude(self, latitude):
  33.         self.latitude = latitude
  34.  
  35.     def set_longitude(self, longitude):
  36.         self.longitude = longitude
  37.  
  38.     def __repr__(self):
  39.         return f"Town: {self.name} | Latitude: {self.latitude} | Longitude: {self.longitude}"
  40.  
  41.  
  42. town = Town("Sofia")
  43. town.set_latitude("42° 41\' 51.04\" N")
  44. town.set_longitude("23° 19\' 26.94\" E")
  45. print(town)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement