Posts: 4
Threads: 1
Joined: Sep 2024
I'm having problems with a couple parts of the Python Crash Course 3rd Edition book. The first problem im having is getting a line with super(). ___init__(make,model, year) getting an error code saying there is no argument for the line. It's chapter 9 page 172. Secondly, is on page 184 Chapter 10. I can't get the file reader to work because it can't find anything in PithLib even after i've downloaded the files from the githib. Any ideas on what I can do or some advice?
Posts: 1,145
Threads: 114
Joined: Sep 2019
It would help if you posted code..
Posts: 6,792
Threads: 20
Joined: Feb 2020
Sep-09-2024, 05:47 PM
(This post was last modified: Sep-09-2024, 05:47 PM by deanhystad.)
Sounds like you are not providing values for all the arguments, but I cannot tell since you did not post code. Even if I had the book (which I don't) I could only guess the error(s) you made. Post your code and the entire error message, including the traceback information.
Posts: 4
Threads: 1
Joined: Sep 2024
Sep-09-2024, 06:32 PM
(This post was last modified: Sep-09-2024, 06:56 PM by deanhystad.)
class Car:
"""A seimple attempt to represent a car."""
def __init__(self, make, model, year):
"""Initialize attributes to desribe car."""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""Set the odometer reading to the given value."""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""Add the given amount to the ododmeter reading."""
self.odometer_reading += miles
class Battery:
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=40):
"""Initialize the battery's attributes."""
self.battery_size = battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")
def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 40:
range = 150
elif self.battery_size == 65:
range = 225
print(f"This car can go about {range} miles on a full charge.")
class ElectricCar(Car):
"""Represents aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(make, model, year)
self.battery = Battery()
def fill_gas_tank(self):
"""Electric cars don't have gas tanks."""
print("This car dones't have a gas tank!")
my_leaf = ElectricCar('nissian', 'leaf', 2024)
print(my_leaf.get_descriptive_name())
my_leaf.battery.descsribe_battery()
my_leaf.battery.get_range()
deanhystad write Sep-09-2024, 06:56 PM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Attached Files
Thumbnail(s)
Posts: 6,792
Threads: 20
Joined: Feb 2020
Sep-09-2024, 08:53 PM
(This post was last modified: Sep-09-2024, 08:53 PM by deanhystad.)
In the future, please post the error messages and trace as text, not images.
Due to an indentation error, class Car does not have any methods.
def __init__(self, make, model, year): # Due to indenting, this is not a method of Car
"""Initialize attributes to desribe car."""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 Indentation is syntax in Python, not just a way to make the code look pretty. The indenting for Battery and ElectricCar is correct.
Posts: 4
Threads: 1
Joined: Sep 2024
Where would the indentation error be at though? I've retyped the code and it still gives me the same problem. Thank you for the hlep.
Posts: 4
Threads: 1
Joined: Sep 2024
Where would the indentation error be at though, I've re typed it and still had the error. Thank you for the help.
Posts: 7,317
Threads: 123
Joined: Sep 2016
Indentation like this,and some typo fixes.
class Car:
"""A simple attempt to represent a car."""
def __init__(self, make, model, year):
"""Initialize attributes 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 descriptive name."""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""Set the odometer reading to the given value."""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
class Battery:
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=40):
"""Initialize the battery's attributes."""
self.battery_size = battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")
def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 40:
range = 150
elif self.battery_size == 65:
range = 225
print(f"This car can go about {range} miles on a full charge.")
class ElectricCar(Car):
"""Represents aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(make, model, year)
self.battery = Battery()
def fill_gas_tank(self):
"""Electric cars don't have gas tanks."""
print("This car doesn't have a gas tank!")
my_leaf = ElectricCar('nissan', 'leaf', 2024)
print(my_leaf.get_descriptive_name())
my_leaf.battery.describe_battery()
my_leaf.battery.get_range() Output: 2024 Nissan Leaf
This car has a 40-kWh battery.
This car can go about 150 miles on a full charge.
|