![]() |
printing/out put issue with class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: printing/out put issue with class (/thread-42748.html) |
printing/out put issue with class - arabuamir - Aug-24-2024 Hi, everyone, i'm new (biggner) to python, started coding from python crash course book, reached now in chapter 9, having and issue with out put, as there is no out put, the result shows that the (program exited with code:0), code is copied below, shows that printing command is given, iwould realy appreciate your help anf guidlines, thank you. class Car(): """A simple attempt to represent a car.""" def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(self, mileage): if mileage >= self.odometer_reading: self.odometer_reading = mileage print("You can't roll back an odometer!") def increment_odometer(self, miles): self.odometer_reading += miles class ElectricCar(Car): """Represent aspects of a car, specific to electric vehicles.""" def __init__(self, make, model, year): """Initialize attributes of the parent class.""" super().__init__(make, model, year) my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla.get_descriptive_name()) RE: printing/out put issue with class - Larz60+ - Aug-24-2024 Indentation is off, resulting in all methods being part of previous method. RE: printing/out put issue with class - deanhystad - Aug-24-2024 Code should look more like this: class Car(): """Base class for cars.""" def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def __str__(self): """Return a pretty description.""" return f"{self.year} {self.make} {self.model}" def __repr__(self): """Return an informative description.""" return f"<{self.__class__.__name__} {self.year} {self.make} {self.model}>" def update_odometer(self, mileage): """Set odometer miles. Don't allow rolling back miles.""" if mileage < self.odometer_reading: raise ValueError("You can't roll back an odometer!") self.odometer_reading = mileage def increment_odometer(self, miles): """Add miles to odometer.""" self.odometer_reading += miles class ElectricCar(Car): """An electric car.""" my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla) print([my_tesla])I corrected the indenting and added vertical spacing. Methods in a class should be separated by 1 blank line. There should be two blank lines before and after a class. Python has some special methods that it uses when operating on classes. Two of these are __str__() and __repr__() which python uses when printing an instance of a class. I replaced get_descriptive_name() with the __str__() method that python calls to get a pretty str appropriate for printing. There is also a __repr__() method that returns a string with more information, in this example it returns the class name in addition to the car description. I removed the ElectricCar.__init__() method. You only need to write an __init__() method when the __init__() method does something differently than the superclass __init__(). Calling super().__init__() and passing along the arguments is not doing something differently. I removed the print statements. It makes a class difficult to use if calling some of the methods produces output that might interfere with the user interface. In the update_odometer() I replaced the print command with raising an exception. Now, someone using your class can capture the exception and do something different than printing to the terminal. Maybe they are writing a GUI application in tkinter, and they want to display exceptions in a message box. I dropped the read_odometer class. If a program wants to display the odometer reading it can get the value from the odometer_reading attribute and print it. I added a docstring to each method that describes what the method does. Don't force people to read code. Spend a few seconds and write a short description of the class and what each of the methods does. RE: printing/out put issue with class - arabuamir - Aug-25-2024 Thank you very much all, for your help instructions and guidlines,i really appreciate you support, as instructed i have copied the solution to editor on my laptop and result was successful and amazing, due to being new and first time posting for help on python forum i may have made some mistake unknowingly, that because i ahve not understood some instruction or how to go about it, hopefullly you will excuse me for that. i am posting the recieved solution/code from python member, for my issue, once again thanks to all: class Car(): """Base class for cars.""" def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def __str__(self): """Return a pretty description.""" return f"{self.year} {self.make} {self.model}" def __repr__(self): """Return an informative description.""" return f"<{self.__class__.__name__} {self.year} {self.make} {self.model}>" def update_odometer(self, mileage): """Set odometer miles. Don't allow rolling back miles.""" if mileage < self.odometer_reading: raise ValueError("You can't roll back an odometer!") self.odometer_reading = mileage def increment_odometer(self, miles): """Add miles to odometer.""" self.odometer_reading += miles class ElectricCar(Car): """An electric car.""" my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla) print([my_tesla])out put:
|