class Restaurant():
""" 描述一个饭店"""
def _init_(self,name,c_type):
"""初始化饭店信息"""
self.name=name
self.type=c_type
def describe_res(self):
print("The restaurant's name is "+self.name+", and its type is "
+self.type)
def open_restaurant(self):
print("The restaurant is open")
res=Restaurant('a','Chinese food')
print(res.name)
print(res.type)
res.describe_res()
res.open_restaurant()
9-2
res1=Restaurant('b','Eng')
res2=Restaurant('c','Frh')
res3=Restaurant('d','Ita')
res1.describe_res()
res2.describe_res()
res3.describe_res()
9-4
class Restaurant():
""" 描述一个饭店"""
def _init_(self,name,c_type):
"""初始化饭店信息"""
self.name=name
self.type=c_type
self.num_served=0
def describe_res(self):
print("The restaurant's name is "+self.name+", and its type is "
+self.type)
def open_restaurant(self):
print("The restaurant is open")
def set_num_served(self,num):
self.num_served=num
def incre_num_served(self,inc):
self.num_served+=inc
res=Restaurant('a','chn')
print(res.num_served)
res.set_num_served(3)
print(res.num_served)
res.incre_num_served(5)
print(res.num_served)
9-6
class Restaurant():
""" 描述一个饭店"""
def _init_(self,name,c_type):
"""初始化饭店信息"""
self.name=name
self.type=c_type
self.num_served=0
def describe_res(self):
print("The restaurant's name is "+self.name+", and its type is "
+self.type)
def open_restaurant(self):
print("The restaurant is open")
def set_num_served(self,num):
self.num_served=num
def incre_num_served(self,inc):
self.num_served+=inc
class IceCreamStand(Restaurant):
def _init_(self,name,type):
super()._init_(name,type)
self.flavors=['banana','apple','pear']
def print_ices(self):
print('There are flavors as follows')
for flavor in self.flavors:
print(flavor)
ice=IceCreamStand('a','ice cream')
ice.print_ices()
9-13
from collections import OrderedDict
dict=OrderedDict()
dict['a']='111'
dict['b']='222'
dict['c']='555'
for key,value in dict.items():
print(key+' '+value)
10-1
filename='python_learning'
with open(filename) as f_obj:
learning=f_obj.read()
print(learning)
print('\n')
for line in f_obj:
print(line)
lines=f_obj.readlines()
for line in lines:
print(line)
10-2
with open('pythom_learning') as f_obj:
for line in f_obj:
line.replace('python','C')
print(line)
10-3
name=input('Please input your name:')
with open('guest.txt','w') as f_obj:
f_obj.write(name)
10-6
a=input('Input the first number:')
b=input('Input the second number')
try:
c=int(a)+int(b)
except TypeError:
print('You should input numbers')
else:
print(c)
10-11
num=input('Input your favorate number:')file_name='num.json'
with open(file_name,'w') as f_obj:
json.dump(num,f_obj)
file_name='num.json'
with open(file_name) as f_obj:
num=json.load(f_obj)
print('I know Your Favorate number is ' + num)