[python 作业] [第五周]

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

代码:

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

    def describe_restaurant(self):
        print("Restaurant Nmae: " + self.restaurant_name)
        print("Cuisine Type: " + self.cuisine_type)

    def open_restaurant(self):
        print("Restaurant is open, welconme!")

my_res =  Restaurant("Great Fire", "HotPot")
print(my_res.restaurant_name)
print(my_res.cuisine_type, end='\n\n')

my_res.describe_restaurant()
print()
my_res.open_restaurant()

测试结果:

Great Fire
HotPot

Restaurant Nmae: Great Fire
Cuisine Type: HotPot

Restaurant is open, welconme!

9-2 三家餐馆:根据你为完成练习 9-1 而编写的类创建三个实例,并对每个实例调
用方法 describe_restaurant()。
and
9-10 导入 Restaurant 类:将最新的 Restaurant 类存储在一个模块中。在另一个文
件中,导入 Restaurant 类,创建一个 Restaurant 实例,并调用 Restaurant 的一个方法,
以确认 import 语句正确无误。

代码:

from Restaurant import Restaurant

res_a = Restaurant("Great Fire", "HotPot")
res_b = Restaurant("YangXiaoXian", "Desert")
res_c = Restaurant("MacDonald", "Fast food")
res_a.describe_restaurant()
print()
res_b.describe_restaurant()
print()
res_c.describe_restaurant()
print()

测试结果:

Restaurant Nmae: Great Fire
Cuisine Type: HotPot

Restaurant Nmae: YangXiaoXian
Cuisine Type: Desert

Restaurant Nmae: MacDonald
Cuisine Type: Fast food

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

代码:

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

    def describe_restaurant(self):
        print("Restaurant Nmae: " + self.restaurant_name)
        print("Cuisine Type: " + self.cuisine_type)

    def open_restaurant(self):
        print("Restaurant is open, welconme!")

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

    def increment_number_served(self, add):
        self.number_served += add
        return self.number_served


my_res =  Restaurant("Great Fire", "HotPot", 2)
print('Number served: ' + str(my_res.number_served))
my_res.increment_number_served(5)
print('Number served(after update): ' + str(my_res.number_served))
my_res.increment_number_served(200)
print('Number served(a day): ' + str(my_res.number_served))

测试结果:

Number served: 2
Number served(after update): 7
Number served(a day): 207

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

代码:

from Restaurant import Restaurant

class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type, number_served=0, flavors=['milk', 'matcha', 'durian', 'chocolate']):
        super().__init__(restaurant_name, cuisine_type, number_served=0)
        self.flavors = flavors

    def show_flavors(self):
        print("All the Ice Cream flavors we can offer:\n", self.flavors)

my_res = IceCreamStand('Pretty Ice', 'Ice Cream', 50)
my_res.show_flavors()

测试结果:

All the Ice Cream flavors we can offer:
 ['milk', 'matcha', 'durian', 'chocolate']


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

代码:

with open ('learning_python.txt', 'r') as lp:
    print(lp.read(), end='\n\n')

with open ('learning_python.txt', 'r') as lp:
    for line in lp:
        print(line, end='')
    print('\n')

with open ('learning_python.txt', 'r') as lp:
    ls = []
    for line in lp:
        ls.append(line)
    for x in ls:
        print(x, end='')

测试结果:

In Python you can use 'range()' to generate lists quickly.
In Python you can use 'strip()' to get rip of blank spaces.
In Pyhont you can use 'type()' to know the type of the variable.

In Python you can use 'range()' to generate lists quickly.
In Python you can use 'strip()' to get rip of blank spaces.
In Pyhont you can use 'type()' to know the type of the variable.

In Python you can use 'range()' to generate lists quickly.
In Python you can use 'strip()' to get rip of blank spaces.
In Pyhont you can use 'type()' to know the type of the variable.

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

代码:

while True:
    name = input('Please input your name(q to quit): ')
    if name.lower() == 'q':
        break
    else:
        with open('guest_book.txt', 'a') as gb:
            gb.write(name+'\n')

输入:

Please input your name(q to quit): Andy
Please input your name(q to quit): Bill
Please input your name(q to quit): Cary
Please input your name(q to quit): Denny
Please input your name(q to quit): Edward
Please input your name(q to quit): q

输出(guest_book.txt):

Andy
Bill
Cary
Denny
Edward

10-6 加法运算:提示用户提供数值输入时,常出现的一个问题是,用户提供的是
文本而不是数字。在这种情况下,当你尝试将输入转换为整数时,将引发 TypeError 异
常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。在用户输入的
任何一个值不是数字时都捕获 TypeError 异常,并打印一条友好的错误消息。对你编写
的程序进行测试:先输入两个数字,再输入一些文本而不是数字。

代码:

a = int(input('Please input num 1: '))
b = input('Please input num 2: ')
try:
    c = a + b
except TypeError:
    c = int
print('num c = ', c)

测试结果:

Please input num 1: 1
Please input num 2: 2
num c =  3

10-11 喜欢的数字:编写一个程序,提示用户输入他喜欢的数字,并使用
json.dump()将这个数字存储到文件中。再编写一个程序,从文件中读取这个值,并打
印消息“I know your favorite number! It’s _.”。

代码1:

import json

favorite_num = input("What's your favorite number: ")

file_name = 'favorite_number.txt'
with open(file_name, 'w') as f_obj:
    json.dump(favorite_num, f_obj)

测试结果1:

What's your favorite number: 11

代码2:

import json

file_name = 'favorite_number.txt'

with open(file_name, 'r') as f_obj:
    temp = json.load(f_obj)
    print("I know your favorite number! It's " + temp)

测试结果2:

I know your favorite number! It's 11

猜你喜欢

转载自blog.csdn.net/ill__world/article/details/79823674