Working through python crash course and got to classes. I’m following along and running all the code provided, however, I cant get one method (update_odometer) to run correctly.
It should provide an error if I try to set the odometer to a lower number than it is, but I can only get that number if I update it to a negative number of miles.
Does running the Car class reset odometer_reading to 0 each time it is ran? That is what it seems like, however, I think I have everything copied exactly from the book.
class Car: """A simple attempt to describe a car""" def __init__(self, make, model, year): """Initilize attribues to describe a car""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): """Return a neatly formatted name""" long_name = f"{self.year} {self.make} {self.model}" return long_name.title() def update_odometer(self, miles): """Set odometer to a given value, reject the change if it attempts to roll back the odometer""" if miles < self.odometer_reading: print("No rolling back unless your Danny Devito!") else: self.odometer_reading = miles def increment_odometer(self, miles): """Incrememnt the odometer a given amount""" self.odometer_reading += miles def read_odometer(self): """Print a message with the cars odometer reading.""" msg = f"This car has {self.odometer_reading} miles on it." print(msg) my_new_car = Car('audi', 'a4', 2024)
submitted by /u/Skuttlebutt42
[link] [comments]
r/learnpython Working through python crash course and got to classes. I’m following along and running all the code provided, however, I cant get one method (update_odometer) to run correctly. It should provide an error if I try to set the odometer to a lower number than it is, but I can only get that number if I update it to a negative number of miles. Does running the Car class reset odometer_reading to 0 each time it is ran? That is what it seems like, however, I think I have everything copied exactly from the book. class Car: “””A simple attempt to describe a car””” def __init__(self, make, model, year): “””Initilize attribues to describe a car””” self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): “””Return a neatly formatted name””” long_name = f”{self.year} {self.make} {self.model}” return long_name.title() def update_odometer(self, miles): “””Set odometer to a given value, reject the change if it attempts to roll back the odometer””” if miles < self.odometer_reading: print(“No rolling back unless your Danny Devito!”) else: self.odometer_reading = miles def increment_odometer(self, miles): “””Incrememnt the odometer a given amount””” self.odometer_reading += miles def read_odometer(self): “””Print a message with the cars odometer reading.””” msg = f”This car has {self.odometer_reading} miles on it.” print(msg) my_new_car = Car(‘audi’, ‘a4’, 2024) submitted by /u/Skuttlebutt42 [link] [comments]
Working through python crash course and got to classes. I’m following along and running all the code provided, however, I cant get one method (update_odometer) to run correctly.
It should provide an error if I try to set the odometer to a lower number than it is, but I can only get that number if I update it to a negative number of miles.
Does running the Car class reset odometer_reading to 0 each time it is ran? That is what it seems like, however, I think I have everything copied exactly from the book.
class Car: """A simple attempt to describe a car""" def __init__(self, make, model, year): """Initilize attribues to describe a car""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): """Return a neatly formatted name""" long_name = f"{self.year} {self.make} {self.model}" return long_name.title() def update_odometer(self, miles): """Set odometer to a given value, reject the change if it attempts to roll back the odometer""" if miles < self.odometer_reading: print("No rolling back unless your Danny Devito!") else: self.odometer_reading = miles def increment_odometer(self, miles): """Incrememnt the odometer a given amount""" self.odometer_reading += miles def read_odometer(self): """Print a message with the cars odometer reading.""" msg = f"This car has {self.odometer_reading} miles on it." print(msg) my_new_car = Car('audi', 'a4', 2024)
submitted by /u/Skuttlebutt42
[link] [comments]