python homework ——the fifth week

本文介绍了使用Python定义餐厅类的过程,并通过多个实例展示了如何设置和更新餐厅信息,包括名称、类型和服务人数等属性。此外,还扩展了一个冰淇淋店子类,演示了继承和特定功能的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

9-1
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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值