python编程从入门到实践-第九章习题

动手试一试


9.1餐馆

class Restaurant():
    def __init__(self,name,type):
        self.name=name;
        self.type=type;
    def describe_restaurant(self):
        print("The restaurant name is "+self.name+" !");
        print("The restaurant type is "+self.type+" !");
    def open_restaurant(self):
        print("The restaurant is running!");
a=Restaurant("guogang","S");
Restaurant.describe_restaurant(a);
Restaurant.open_restaurant(a)

9.3用户

class use():
    def __init__(self,firstname,lastname):
        self.firstname=firstname;
        self.lastname=lastname;
    def describe_use(self):
        print("The use name is "+self.firstname+","+self.lastname+" !");
    def greet_use(self):
        print("hello!"+self.firstname+","+self.lastname);
a=use("guo","gang");
a.describe_use();
a.greet_use();

9.4就餐人数

class Restaurant():
    def __init__(self,name,type):
        self.name=name;
        self.type=type;
        self.account=0;
    def describe_restaurant(self):
        print("The restaurant name is "+self.name+" !");
        print("The restaurant type is "+self.type+" !");
    def open_restaurant(self):
        print("The restaurant is running!");
    def set_number(self,account):
        self.account=account;
    def inc_number(self,and1):
        if and1 > 0:
            self.account+=and1;
a=Restaurant("guogang","001");
a.set_number(10);
a.inc_number(5);
print(a.account);

9.6冰淇淋小店

class Restaurant():
    def __init__(self,name,type):
        self.name=name;
        self.type=type;
        self.account=0;
    def describe_restaurant(self):
        print("The restaurant name is "+self.name+" !");
        print("The restaurant type is "+self.type+" !");
    def open_restaurant(self):
        print("The restaurant is running!");
    def set_number(self,account):
        self.account=account;
    def inc_number(self,and1):
        if and1 > 0:
            self.account+=and1;
class Iceroom(Restaurant):
    def __init__(self,name,type):
        super().__init__(name,type)
        self.fav=[];
    def faver(self,ice):
        for cell in ice:
            self.fav.append(cell);
a=Iceroom("guogang","001");
b=["A","B","C"];
a.faver(b);
print(a.fav);

9.10导入Restaurant类
-Restaurant与Iceroom类的模块

class Restaurant():
    def __init__(self,name,type):
        self.name=name;
        self.type=type;
        self.account=0;
    def describe_restaurant(self):
        print("The restaurant name is "+self.name+" !");
        print("The restaurant type is "+self.type+" !");
    def open_restaurant(self):
        print("The restaurant is running!");
    def set_number(self,account):
        self.account=account;
    def inc_number(self,and1):
        if and1 > 0:
            self.account+=and1;
class Iceroom(Restaurant):
    def __init__(self,name,type):
        super().__init__(name,type)
        self.fav=[];
    def faver(self,ice):
        for cell in ice:
            self.fav.append(cell);
a=Iceroom("guogang","001");
b=["A","B","C"];
a.faver(b);
print(a.fav);

-new-restanrant模块

from Restaurant import Iceroom
d=Iceroom("guogang","001");

9.14骰子

from random import randint
class Die():
    def __init__(self,account):
        self.account=account;
    def roll1(self,account):
        x=randint(1,account);
        return x;
a=Die(6);
count=10;
while count > 0:
    print(a.roll1(6));
    count-=1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值