《Python编程-从入门到实践》第九章习题训练

本章知识点:

1.如何编写类。

2.如何使用属性在类中存储信息,编写方法。

3.如何编写方法__init()。

4.使用继承可简化相关类的创建工作。

5.将一个类的实例用作另一个类的属性。

6.掌握python标准库的模块。


习题:

9-1餐馆  9-4就餐人数

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

    def describe_restaurant(self):
        print('The name is '+ self.name)
        print('The type is '+ self.type)

    def open_restaurant(self):
        print('The restaurant is opening.')

    def set_number_served(self,number_serve):
        self.num = number_serve

    def increment_number_sreved(self):
        self.num = self.num+1
        return 30


restaurant = Restaurant('Tai','noddle')
restaurant.describe_restaurant()
restaurant.open_restaurant()

restaurant4 = Restaurant('Gang','XiCan',10)
print('There are '+str(restaurant4.num)+' people eating.')
restaurant4.num = 20
print('There are '+str(restaurant4.num)+' people eating.')

restaurant5 = Restaurant('XingLong','XiCan')
restaurant5.set_number_served(10)
print(restaurant5.num)
print(restaurant5.increment_number_sreved() )
print(restaurant5.num)

输出:

The name is Tai
The type is noddle
The restaurant is opening.
There are 10 people eating.
There are 20 people eating.
10
30
11

9-2三家餐馆

restaurant1 = Restaurant('Little','Rice')
restaurant1.describe_restaurant()
restaurant2 = Restaurant('XinFu','XiaoChi')
restaurant2.describe_restaurant()
restaurant3 = Restaurant('MaiXiang','Xinliao')
restaurant3.describe_restaurant()

输出:

The name is Little
The type is Rice
The name is XinFu
The type is XiaoChi
The name is MaiXiang
The type is Xinliao

9-3用户 9-5尝试登陆次数

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

    def describe_inf(self):
        print('Name: '+self.first_name+' , '+self.last_name)
        print('age: '+str(self.age))

    def greet_user(self):
        print('Hello, '+ self.first_name + '. Nice to meet you! ')

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

    def reset_login_attempts(self):
        self.login_attempts = 0

my_user = User('huang','zi',18)
my_user.describe_inf()
my_user.greet_user()

my_user1 = User('Zhang','Gen',20,0)
num = 1
while num <= 5:
    num = num+1
    my_user1.increment_login_attempts()
print(my_user1.login_attempts)
my_user1.reset_login_attempts()
print(my_user1.login_attempts)

输出:

Name: huang , zi
age: 18
Hello, huang. Nice to meet you! 
5
0

9-6冰淇淋小店

class IceCreamStand(Restaurant):
    def __init__(self,restaurant_name,cuisine_type,number_served,flavors):
        super().__init__(restaurant_name,cuisine_type,number_served)
        self.flavors = flavors

    def print_flavors(self):
        print('The flavors are : '+self.flavors)

ice_cream_stand = IceCreamStand('Love','ZhonCan',20,'Juice')
ice_cream_stand.print_flavors()
print(ice_cream_stand.num)
ice_cream_stand.describe_restaurant()

输出:

The flavors are : Juice
20
The name is Love
The type is ZhonCan

9-7管理员 9-8权限

class Privileges():
    def __init__(self,privileges):
        self.privileges = privileges

    def show(self):
        print('The privileges is '+self.privileges)


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

admin = Admin('huang','zi',20,2,'can add post')
admin.privileges.show()

输出:

The privileges can add post.

9-10导入Restaurant类

from class1 import Restaurant
restaurant = Restaurant('Gang','XiCan',10)
restaurant.describe_restaurant()

输出:

The name is Gang
The type is XiCan

9-11导入Admin类

from class1 import Admin
admin = Admin('huang','zi',20,2,'can add post')
admin.privileges.show()

输出:

The privileges can add post

9-13使用OrderedDict

from collections import OrderedDict

item = OrderedDict()

item['First']='1'
item['Second']='2'
item['Third']='3'
item['Fourth']='4'
print(item)

输出:

OrderedDict([('First', '1'), ('Second', '2'), ('Third', '3'), ('Fourth', '4')])
9-14
from random import randint
class Die():
    def __init__(self,sides=0):
        self.sides = sides

    def roll_die(self):
        num = randint(1,self.sides)
        print('The number is '+str(num) )

die = Die(6)
i=1
while i <= 5:
    i = i+1
    die.roll_die()

输出:

The number is 2
The number is 4
The number is 3
The number is 2
The number is 3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值