导包 -python基础

car.py

class 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
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        self.odometer_reading += miles


class Battery():
    def __init__(self, battery_size=60):
        self.battery_size = battery_size

    def describe_battery(self):
        print("This car has a " + str(self.battery_size) + "-kwh battery.")

    def get_range(self):
        if self.battery_size == 70:
            range = 240
        elif self.battery_size == 85:
            range = 270
        message = "This car can go approximately " + str(range)
        message += " miles on a full charge."
        print(message)


class ElectricCar(Car):
    def __init__(self, make, model, year):

        super().__init__(make, model, year)
        self.battery = Battery()

my_car.py

from car import Car, ElectricCar

my_beetle = Car("volkswagen", "beetle", 2016)
print(my_beetle.get_descriptive_name())  # 2016 Volkswagen Beetle
my_tesla = ElectricCar("tesla", "roadster", 2016)
print(my_tesla.get_descriptive_name())  # 2016 Tesla Roadster


import car
my_beetle = car.Car("volkswagen", "beetle", 2016)
print(my_beetle.get_descriptive_name())  # 2016 Volkswagen Beetle
my_tesla = car.ElectricCar("tesla", "roadster", 2016)
print(my_tesla.get_descriptive_name())  # 2016 Tesla Roadster

pizza.py

def make_pizza(size, *toppings):
    print("\nMaking a "+str(size)+"-inch pizza with the following topppings:")
    for topping in toppings:
        print("- " + topping)

making_pizza.py

import pizza

pizza.make_pizza(16, "pepperoni")
# Making a 16-inch pizza with the following topppings:
# - pepperoni
pizza.make_pizza(12, "mushrooms", "green peppers", "extra cheese")
# Making a 12-inch pizza with the following topppings:
# - mushrooms
# - green peppers
# - extra cheese

from pizza import make_pizza

make_pizza((16, "pepperoni"))
# Making a (16, 'pepperoni')-inch pizza with the following topppings:
make_pizza(12, "mushroom", "green peppers")
# Making a 12-inch pizza with the following topppings:
# - mushroom
# - green peppers

from pizza import make_pizza as mp

mp(16, "pepperoni")
mp(12, "mushroom", "green peppers", "extra cheese")

import pizza as p

p.make_pizza(16, "pepperoni")
p.make_pizza(12, "mushroom", "green peppers", "extra cheese")

from pizza import *

make_pizza(16, "pepperoni")
make_pizza(12, "mushroom", "green peppers", "extra cheese")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值