第五次作业(4.2/4.4)

第九章作业

9-1 餐馆 :创建一个名为Restaurant 的类,其方法 _ init _() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用前述两个方法。

9-2 三家餐馆 :根据你为完成练习9-1而编写的类创建三个实例,并对每个实例调用方法describe_restaurant() 。

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

class Restaurant(object):
    def __init__(self, name, types, served = 0):
        self.restaurant_name = name
        self.cuisine_type = types
        self.number_served = served
    def describe_restaurant(self):
        print("Restaurant name is %s. Cuisine type is %s\n" % (self.restaurant_name, self.cuisine_type))
    def open_restaurant(self):
        print("The restaurant is open now.\n")

    def print_number_served(self):
        print("%d people have been served here\n" % self.number_served)
        
    def set_number_served(self, number):
        self.number_served = number
        print("The number of people is %d\n" % number)
    def increment_number_served(self, number):
        self.number_served += number

# 9-1
res = Restaurant("A", "a")
print(res.restaurant_name)
print(res.cuisine_type)
res.describe_restaurant()
res.open_restaurant()

# 9-2
res1 = Restaurant("Restaurant1", "Italian")
res2 = Restaurant("Restaurant2", "French")
res3 = Restaurant("Restaurant3", "Chinese")
res1.describe_restaurant()
res2.describe_restaurant()
res3.describe_restaurant()

# 9-4
res4 = Restaurant("Restarant4", "Thai", 10)
res4.print_number_served()
res4.set_number_served(20)
res4.increment_number_served(30)
res4.print_number_served()

输出结果:

A
a
Restaurant name is A. Cuisine type is a

The restaurant is open now.

Restaurant name is Restaurant1. Cuisine type is Italian

Restaurant name is Restaurant2. Cuisine type is French

Restaurant name is Restaurant3. Cuisine type is Chinese

10 people have been served here

The number of people is 20

50 people have been served here

9-6 冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。
编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。

class Restaurant(object):
    def __init__(self, name, types, served = 0):
        self.restaurant_name = name
        self.cuisine_type = types
        self.number_served = served
    def describe_restaurant(self):
        print("Restaurant name is %s. Cuisine type is %s\n" % (self.restaurant_name, self.cuisine_type))
    def open_restaurant(self):
        print("The restaurant is open now.\n")

    def print_number_served(self):
        print("%d people have been served here\n" % self.number_served)
        
    def set_number_served(self, number):
        self.number_served = number
        print("The number of people is %d\n" % number)
    def increment_number_served(self, number):
        self.number_served += number

class IceCreamStand(Restaurant):
    def __init__(self, name, types, m_flavors, served = 0):
        super(IceCreamStand, self).__init__(name, types, served)
        self.flavors = m_flavors
    def show_flavors(self):
        print("The flavor of %s are served." %  ", ".join(self.flavors))
        
ICRes = IceCreamStand(name = "MyRestaurant", types = "Ice Cream", \
                        m_flavors = ["apple", "orange", "strawberry"])

ICRes.describe_restaurant()
ICRes.show_flavors()

输出结果

Restaurant name is MyRestaurant. Cuisine type is Ice Cream

The flavor of apple, orange, strawberry are served.

9-10 导入Restaurant 类 :将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调用Restaurant 的一个方法,以确认import 语句正确无误。

在python-shell环境下运行, 文件名为a.py:

>>> from a import Restaurant
>>> a = Restaurant("test", "test2")
>>> a.describe_restaurant()
Restaurant name is test. Cuisine type is test2

第十章作业

10-1 Python学习笔记 :在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,其中每一行都以“In Python you can”打头。将这个文件命名为learning_python.txt,并将其存储到为完成本章练习而编写的程序所在的目录中。
编写一个程序,它读取这个文件,并将你所写的内容打印三次:第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在with 代码块外打印它们。

info = []
print("Read the whole file:")
with open("learning_python.txt") as fp:
    print(fp.read())
fp.close()
print("Iterate every line:")
with open("learning_python.txt") as fp:
    for s in fp.readlines():
        print(s.strip())
fp.close()
print("Save contents in a list:")
with open("learning_python.txt") as fp:
    info = fp.read().split("\n")
fp.close()
print("\n".join(info))

10-3 访客 :编写一个程序,提示用户输入其名字;用户作出响应后,将其名字写入到文件guest.txt中。

name = input("Please input your name\n")
with open("guest.txt", 'w') as f:
    f.write(name + '\n')

10-4 访客名单 :编写一个while 循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,并将一条访问记录添加到文件guest_book.txt中。确保这个文件中的每条记录都独占一行。

with open("guest_book.txt", 'w') as fp:
    name = ""
    while True:
        name = input("Please input your name(input 'q' to quit):")
        if name=="q":
            break
        fp.write(name + '\n')
        print("Hello", name)
fp.close()

10-6 加法运算 :提示用户提供数值输入时,常出现的一个问题是,用户提供的是文本而不是数字。在这种情况下,当你尝试将输入转换为整数时,将引发TypeError 异常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。在用户输入的任何一个值不是数字时都捕获TypeError 异常,并打印一条友好的错误消息。对你编写的程序进行测试:先输入两个数字,再输入一些文本而不是数字。
10-7 加法计算器 :将你为完成练习10-6而编写的代码放在一个while 循环中,让用户犯错(输入的是文本而不是数字)后能够继续输入数字。

while (1):
    try:
        s = input("Please input the first number:")
        n1 = float(s)
    except:
        print("Please input a correct number.")
    else:
        break
while (1):
    try:
        s = input("Please input the second number:")
        n2 = float(s)
    except:
        print("Please input a correct number.")
    else:
        break
print("%f + %f = %f" % (n1, n2, n1 + n2))

你可能感兴趣的:(第五次作业(4.2/4.4))