Chapter 9
# 9-1 餐馆
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(self.restaurant_name, self.cuisine_type)
def open_restaurant(self):
print('OPEN')
res = Restaurant('AA', 'BB')
print(res.restaurant_name, res.cuisine_type)
res.describe_restaurant()
res.open_restaurant()
AA BB
AA BB
OPEN
# 9-2 三家餐馆
res1 = Restaurant('RESA', 'A')
res2 = Restaurant('RESB', 'B')
res3 = Restaurant('RESC', 'C')
res1.describe_restaurant()
res2.describe_restaurant()
res3.describe_restaurant()
RESA A
RESB B
RESC C
# 9-4 就餐人数
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(self.restaurant_name, self.cuisine_type)
def open_restaurant(self):
print('OPEN')
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self, num):
self.number_served += num
res = Restaurant('A', 'B', 100)
print(res.number_served)
res.set_number_served(120)
print(res.number_served)
res.increment_number_served(10)
print(res.number_served)
100
120
130
# 9-6 冰淇淋小店
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type, number_served=0):
super().__init__(restaurant_name, cuisine_type, number_served)
self.flavors = ['JieMo', 'LiuLian']
def print_flavors(self):
for flavor in self.flavors:
print(flavor)
ICS = IceCreamStand('A', 'B', 100)
ICS.print_flavors()
JieMo
LiuLian
Chapter 10
# 10-1 Python学习笔记
with open('learning_python.txt') as filep:
text = filep.read()
print(text.rstrip())
with open('learning_python.txt') as filep:
for line in filep:
print(line.rstrip())
with open('learning_python.txt') as filep:
lines = filep.readlines()
for line in lines:
print(line.rstrip())
In Python you can A
In Python you can B
In Python you can C
In Python you can A
In Python you can B
In Python you can C
In Python you can A
In Python you can B
In Python you can C
# 10-2 C语言学习笔记
for line in lines:
print(line.replace('Python', 'C').rstrip())
In C you can A
In C you can B
In C you can C
# 10-3 访客
with open('guest.txt', 'w') as fileg:
name = input('Enter your name: ')
fileg.write(name)
Enter your name:
# 10-7 加法计算器
while True:
try:
x = int(input('input x: '))
y = int(input('input y: '))
except ValueError:
print('the input must be digit!')
else:
print(x+y)
break
input x: hhh
the input must be digit!
input x: 4
input y: 96
100