Python编程:从入门到实践第二版答案(第九章)

Python编程:从入门到实践第二版答案(第九章)_第1张图片

class Restaurant:
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(f"This {self.restaurant_name} has {self.cuisine_type}.")

    def open_restaurant(self):
        print(f"{self.restaurant_name},now is open.")


restaurant = Restaurant('BeiJingKaoYa', 'Roast duck')

print(f"{restaurant.restaurant_name}")
print(f"{restaurant.cuisine_type}")
restaurant.describe_restaurant()
restaurant.open_restaurant()

#输出
BeiJingKaoYa
Roast duck
This BeiJingKaoYa has Roast duck.
BeiJingKaoYa,now is open.

class Restaurant:
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(f"This {self.restaurant_name} has {self.cuisine_type}.")

    def open_restaurant(self):
         print(f"{self.restaurant_name},now is open.")


restaurant1 = Restaurant('St.Mandy', 'fride beancurd with slicedpork&pepper')
restaurant2 = Restaurant('Milo SUN', 'braised common carp')
restaurant3 = Restaurant('FOUNT', 'instant boiled sliced mutton')

restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()

#输出
This St.Mandy has fride beancurd with slicedpork&pepper.
This Milo SUN has braised common carp.
This FOUNT has instant boiled sliced mutton.

class User:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def describe_user(self):
        print(f"\nname:{self.first_name.title()} {self.last_name.title()}")
        print(f"age:{self.age}")

    def greet_user(self):
         print(f"Hello! {self.first_name.title()} {self.last_name.title()}")


user1 = User('harden', 'james', 30)
user2 = User('ansu', 'fati', 32)
user3 = User('durant', 'kobe',12)

user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()
user3.describe_user()
user3.greet_user()


#输出
name:Harden James
age:30
Hello! Harden James

name:Ansu Fati
age:32
Hello! Ansu Fati

name:Durant Kobe
age:12
Hello! Durant Kobe

Python编程:从入门到实践第二版答案(第九章)_第2张图片

class Restaurant:
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        print(f"This {self.restaurant_name} has {self.cuisine_type}.")

    def open_restaurant(self):
        print(f"{self.restaurant_name},now is open.")

    def parasite_num(self):
        print(f"There are {self.number_served} people eating here")

    def set_number_served(self, num):
        self.number_served = num
        print(f"There are {self.number_served} people eating here")

    def increment_number_served(self, num_add):
        self.number_served += num_add


restaurant = Restaurant('BeiJingKaoYa', 'Roast duck')
restaurant.parasite_num()
restaurant.set_number_served(20)
restaurant.parasite_num()
restaurant.increment_number_served(10)
restaurant.parasite_num()

#输出
There are 0 people eating here
There are 20 people eating here
There are 20 people eating here
There are 30 people eating here

Python编程:从入门到实践第二版答案(第九章)_第3张图片

class User:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = 0

    def describe_user(self):
        print(f"\nname:{self.first_name.title()} {self.last_name.title()}")
        print(f"age:{self.age}")

    def greet_user(self):
         print(f"Hello! {self.first_name.title()} {self.last_name.title()}")

    def increment_login_attempts(self):
        self.login_attempts += 1
        return self.login_attempts

    def reset_login_attempts(self):
        self.login_attempts = 0
        return self.login_attempts


user = User('harden', 'james', 30)
num1 = user.increment_login_attempts()
num2 = user.reset_login_attempts()
print(num1)
print(num2)


#输出
1
0

Python编程:从入门到实践第二版答案(第九章)_第4张图片

class Restaurant:
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(f"This {self.restaurant_name} has {self.cuisine_type}.")

    def open_restaurant(self):
        print(f"{self.restaurant_name},now is open.")


class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type):
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = ['Orange', 'Strawberry', 'Vanilla']

    def icecream(self):
        for icecm in self.flavors:
            print(icecm)


icecream = IceCreamStand('ccc', 'aaa')
icecream.icecream()

#输出
Orange
Strawberry
Vanilla

 Python编程:从入门到实践第二版答案(第九章)_第5张图片

class User:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def describe_user(self):
        print(f"\nname:{self.first_name.title()} {self.last_name.title()}")
        print(f"age:{self.age}")

    def greet_user(self):
         print(f"Hello! {self.first_name.title()} {self.last_name.title()}")


class Admin(User):
    def __init__(self, first_name, last_name, age):
        super().__init__(first_name, last_name, age)
        self.privileges = ["can add post", "can delete post", "can ban user"]

    def show_privileges(self):
        for Permissions in self.privileges:
            print(Permissions)


user = Admin('harden', 'james', 30)
user.show_privileges()

#输出
can add post
can delete post
can ban user

Python编程:从入门到实践第二版答案(第九章)_第6张图片

class User:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def describe_user(self):
        print(f"\nname:{self.first_name.title()} {self.last_name.title()}")
        print(f"age:{self.age}")

    def greet_user(self):
         print(f"Hello! {self.first_name.title()} {self.last_name.title()}")


class Admin(User):
    def __init__(self, first_name, last_name, age):
        super().__init__(first_name, last_name, age)
        self.privileges = Privileges()


class Privileges:
    def __init__(self):
        self.privileges = ["can add post", "can delete post", "can ban user"]

    def show_privileges(self):
        for Permissions in self.privileges:
            print(Permissions)


user = Admin('harden', 'james', 30)
user.privileges.show_privileges()


#输出
can add post
can delete post
can ban user

 Python编程:从入门到实践第二版答案(第九章)_第7张图片

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 = f"{self.year} {self.make} {self.model}"
        return long_name.title()


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

    def descriptive_battery(self):
        print(f"This car has a {self.battery_size}-kwh battery.")

    def get_range(self):
        if self.battery_size == 75:
            ranges = 260
        elif self.battery_size == 100:
            ranges = 315

        print(f"This car can go about {ranges} miles on a full charge.")

    def upgrade_battery(self):
        if self.battery_size != 100:
            self.battery_size = 100


class ElectricCar(Car):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery = Battery()


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

my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()



#输出
This car can go about 260 miles on a full charge.
This car can go about 315 miles on a full charge.

from restaurant import Restaurant

restaurant = Restaurant('BeiJingKaoYa', 'Roast duck')
restaurant.parasite_num()
restaurant.set_number_served(20)
restaurant.parasite_num()
restaurant.increment_number_served(10)
restaurant.parasite_num()

#输出
There are 0 people eating here
There are 20 people eating here
There are 20 people eating here
There are 30 people eating here

from admin import Admin


user = Admin('harden', 'james', 30)
user.privileges.show_privileges()


#输出
can add post
can delete post
can ban user

#第一个文件添加 切记!
from user import User

#主文件

from admin import Admin


user = Admin('harden', 'james', 30)
user.privileges.show_privileges()



#输出
同上

Python编程:从入门到实践第二版答案(第九章)_第8张图片

from random import randint


class Die:
    def __init__(self, sides=6):
        self.sides = sides

    def roll_die(self):
        return randint(1, self.sides)


die = Die()


for i in range(10):
    print(die.roll_die())

die10 = Die(sides=10)

for i in range(10):
    print(die10.roll_die())


die20 = Die(sides=20)

for i in range(10):
    print(die20.roll_die())

from random import choice


output = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'C', 'F', 'G', 'H')
lottery = []
for i in range(4):
    outputs = choice(output)
    lottery.append(outputs)


print(f"The winning number is {lottery}")


#输出
The winning number is ['F', '8', '9', '5']

 

from random import choice

m = 1
output = ('1', '2', '3', '4', '5', '6')
lottery = []
flag = True
while flag:
    outputs = choice(output)
    if outputs == '1':
        outputs = choice(output)
        if outputs == '3':
            outputs = choice(output)
            if outputs == '5':
                print("Your are winner!")
                print(m)
                break
            else:
                m +=1
        else:
            m += 1
    else:
        m += 1



#输出
389

你可能感兴趣的:(python,开发语言)