python软件是什么原因引起的_python,这个程序是什么情况?

这样修改

原书上代码是这样的Python Crash Course​ehmatthes.github.io

class Car():

"""A simple attempt to represent a car."""

def __init__(self, manufacturer, model, year):

"""Initialize attributes to describe a car."""

self.manufacturer = manufacturer

self.model = model

self.year = year

self.odometer_reading = 0

def get_descriptive_name(self):

"""Return a neatly formatted descriptive name."""

long_name = str(self.year) + ' ' + self.manufacturer + ' ' + self.model

return long_name.title()

def read_odometer(self):

"""Print a statement showing the car's mileage."""

print("This car has " + str(self.odometer_reading) + " miles on it.")

def update_odometer(self, mileage):

"""

Set the odometer reading to the given value.

Reject the change if it attempts to roll the odometer back.

"""

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=60):

"""Initialize the batteery's attributes."""

self.battery_size = battery_size

def describe_battery(self):

"""Print a statement describing the battery size."""

print("This car has a " + str(self.battery_size) + "-kWh battery.")

def get_range(self):

"""Print a statement about the range this battery provides."""

if self.battery_size == 60:

range = 140

elif self.battery_size == 85:

range = 185

message = "This car can go approximately " + str(range)

message += " miles on a full charge."

print(message)

def upgrade_battery(self):

"""Upgrade the battery if possible."""

if self.battery_size == 60:

self.battery_size = 85

print("Upgraded the battery to 85 kWh.")

else:

print("The battery is already upgraded.")

class ElectricCar(Car):

"""Models aspects of a car, specific to electric vehicles."""

def __init__(self, manufacturer, model, year):

"""

Initialize attributes of the parent class.

Then initialize attributes specific to an electric car.

"""

super().__init__(manufacturer, model, year)

self.battery = Battery()

print("Make an electric car, and check the battery:")

my_tesla = ElectricCar('tesla', 'model s', 2016)

my_tesla.battery.describe_battery()

print("\nUpgrade the battery, and check it again:")

my_tesla.battery.upgrade_battery()

my_tesla.battery.describe_battery()

print("\nTry upgrading the battery a second time.")

my_tesla.battery.upgrade_battery()

my_tesla.battery.describe_battery()Python Crash Courseclass Car():

"""A simple attempt to represent a car."""

def __init__(self, manufacturer, model, year):

"""Initialize attributes to describe a car."""

self.manufacturer = manufacturer

self.model = model

self.year = year

self.odometer_reading = 0

def get_descriptive_name(self):

"""Return a neatly formatted descriptive name."""

long_name = str(self.year) + ' ' + self.manufacturer + ' ' + self.model

return long_name.title()

def read_odometer(self):

"""Print a statement showing the car's mileage."""

print("This car has " + str(self.odometer_reading) + " miles on it.")

def update_odometer(self, mileage):

"""

Set the odometer reading to the given value.

Reject the change if it attempts to roll the odometer back.

"""

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=60):

"""Initialize the batteery's attributes."""

self.battery_size = battery_size

def describe_battery(self):

"""Print a statement describing the battery size."""

print("This car has a " + str(self.battery_size) + "-kWh battery.")

def get_range(self):

"""Print a statement about the range this battery provides."""

if self.battery_size == 60:

range = 140

elif self.battery_size == 85:

range = 185

message = "This car can go approximately " + str(range)

message += " miles on a full charge."

print(message)

def upgrade_battery(self):

"""Upgrade the battery if possible."""

if self.battery_size == 60:

self.battery_size = 85

print("Upgraded the battery to 85 kWh.")

else:

print("The battery is already upgraded.")

class ElectricCar(Car):

"""Models aspects of a car, specific to electric vehicles."""

def __init__(self, manufacturer, model, year):

"""

Initialize attributes of the parent class.

Then initialize attributes specific to an electric car.

"""

super().__init__(manufacturer, model, year)

self.battery = Battery()

print("Make an electric car, and check the battery:")

my_tesla = ElectricCar('tesla', 'model s', 2016)

my_tesla.battery.describe_battery()

print("\nUpgrade the battery, and check it again:")

my_tesla.battery.upgrade_battery()

my_tesla.battery.describe_battery()

print("\nTry upgrading the battery a second time.")

my_tesla.battery.upgrade_battery()

my_tesla.battery.describe_battery()

你将class ElectricCar 中的self.battery 修改成self.battery_size = Battery()

所以最后调用的时候,也需要将

my_tesla.battery.describe_battery()

修改为

my_tesla.battery_size.describe_battery()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值