作业九——类

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 03:20   789   0

该章主要讲述了如何使用类,构造类,还有关于类的继承的问题。

9-1 餐馆

题目描述:

创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名
为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。

INPUT:

None

OUTPUT:

None

代码展示:

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

    def describe_restaurant(self):
        print("The name of this restaurant is " + self.restaurant_name + ".")
        print("The cuisine type of this restaurant is " + self.cuisine_type + ".")

    def open_restaurant(self):
        print(self.restaurant_name.title() + " is open now!")

9-4 就餐人数

题目描述:

在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。
添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。

INPUT:

None

OUTPUT:

The name of this restaurant is Sunny.
The cuisine type of this restaurant is BBQ.
The number of this restaurant served is 0.

After setting:
The name of this restaurant is Sunny.
The cuisine type of this restaurant is BBQ.
The number of this restaurant served is 59.

After increment:
The name of this restaurant is Sunny.
The cuisine type of this restaurant is BBQ.
The number of this restaurant served is 60.

代码展示:

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("The name of this restaurant is " + self.restaurant_name + ".")
        print("The cuisine type of this restaurant is " + self.cuisine_type + ".")
        print("The number of this restaurant served is " + str(self.number_served) + ".")

    def open_restaurant(self):
        print(self.restaurant_name.title() + " is open now!")

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

    def increment_number_served(self):
        self.number_served = self.number_served + 1


SunnyRestaurant = Restaurant("Sunny", "BBQ")
SunnyRestaurant.describe_restaurant()

SunnyRestaurant.set_number_served(59)
print("\nAfter setting:")
SunnyRestaurant.describe_restaurant()

SunnyRestaurant.increment_number_served()
print("\nAfter increment:")
SunnyRestaurant.describe_restaurant()


9-13 使用OrderedDict

题目描述:

在练习6-4中,你使用了一个标准字典来表示词汇表。请使用OrderedDict 类来重写这个程序,并确认输出的顺序与你在字典中添加键
—值对的顺序一致。

INPUT:

None

OUTPUT:

Cuisine : a style or method of cooking, especially as characteristic of a particular country, region, or establishment.
Strict : demanding that rules concerning behavior are obeyed and observed.
Confine : keep or restrict someone or something within certain limits of
Quick : moving fast or doing something in a short time.
Language : The system of communication used by a particular community or country.

代码展示:

from collections import OrderedDict

dictionary = OrderedDict()
dictionary['cuisine'] = 'a style or method of cooking, especially as characteristic ' \
                        'of a particular country, region, or establishment.'
dictionary['strict'] = 'demanding that rules concerning behavior are obeyed and observed.'
dictionary['confine'] = 'keep or restrict someone or something within certain limits ' \
                        'of'
dictionary['quick'] = 'moving fast or doing something in a short time.'
dictionary['language'] = 'The system of communication used by a particular community or ' \
                         'country.'

for key, item in dictionary.items():
    print(key.title() + " : " + item)

9-14 骰子

题目描述:

模块random 包含以各种方式生成随机数的函数,其中的randint() 返回一个位于指定范围内的整数,例如,下面的代码返回一个1~6内的整数:
from random import randint
x = randint(1, 6)
请创建一个Die 类,它包含一个名为sides 的属性,该属性的默认值为6。编写一个名为roll_die() 的方法,它打印位于1和骰子面数之间的随机数。创建一个6面的骰子,再掷10次。 创建一个10面的骰子和一个20面的骰子,并将它们都掷10次。

INPUT:

None

OUTPUT:

This time is 4 !
This time is 4 !
This time is 4 !
This time is 6 !
This time is 6 !
This time is 5 !
This time is 5 !
This time is 5 !
This time is 1 !
This time is 4 !
This time is 5 !
This time is 1 !
This time is 1 !
This time is 2 !
This time is 5 !
This time is 1 !
This time is 3 !
This time is 3 !
This time is 2 !
This time is 2 !

代码展示:

from random import randint

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

    def roll_die(self):
        print("This time is " + str(randint(1, 6)) + " !")


tmp = Die()
for i in range(0, 20):
    tmp.roll_die()
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP