教材第九章习题

9-1 餐馆

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

	def describe_restaurant(self):
		print("The name of the restaurant is " + self.restaurant_name 
			+ "and the cuisine_type is " + self.cuisine_type + ".")

	def open_restaurant(self):
              print("The restaurant is open.")

Mc = Restaurant("McDonald's", "fast food")
print(Mc.restaurant_name)
print(Mc.cuisine_type)
Mc.describe_restaurant()
Mc.open_restaurant()

输出

McDonald's
fast food
The name of the restaurant is McDonald'sand the cuisine_type is fast food.
The restaurant is open.

9-2 三家餐馆

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

	def describe_restaurant(self):
		print("The name of the restaurant is " + self.restaurant_name 
			+ "and the cuisine_type is " + self.cuisine_type + ".")

	def open_restaurant(self):
		print("The restaurant is open.")


Mc = Restaurant("McDonald's", "fast food")
Chen = Restaurant("Chen's", "Chinese food")
French = Restaurant("A French restaurant", "French Food")

Mc.describe_restaurant()
Chen.describe_restaurant()
French.describe_restaurant()

输出

The name of the restaurant is McDonald'sand the cuisine_type is fast food.
The name of the restaurant is Chen'sand the cuisine_type is Chinese food.
The name of the restaurant is A French restaurantand the cuisine_type is French Food.

9-3 用户

class User():
	def __init__(self,first_name,last_name,age,sex):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.sex = sex

	def describe_user(self):
		print("first_name : " + self. first_name 
			+ "\nlast_name : " + self.last_name
			+ "\nage : " + self.age
			+ "\nsex : " + self.sex )

	def greet_user(self):
		print("Hello, " + self.first_name + " " + self.last_name + ".")

HS = User("Sheng", "Huang", "19", "male")
JHY = User("HangYu", "Jin", "20", "female")
HS.describe_user()
HS.greet_user()
JHY.describe_user()
JHY.greet_user()

输出

first_name : Sheng
last_name : Huang
age : 19
sex : male
Hello, Sheng Huang.
first_name : HangYu
last_name : Jin
age : 20
sex : female
Hello, HangYu Jin.

9-4 就餐人数

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

	def describe_restaurant(self):
		print("The name of the restaurant is " + self.restaurant_name 
			+ "and the cuisine_type is " + self.cuisine_type + ".")

	def open_restaurant(self):
		print("The restaurant is open.")

	def set_number_served(self,number):
		self.number_served = number

	def increment_number_served(self):
		self.number += 1


Mc = Restaurant("McDonald\'s", "fast food")
print("There are " + str(Mc.number_served) + " people eating here.")
Mc.number_served = 100
print("There are " + str(Mc.number_served) + " people eating here.")
Mc.set_number_served(12)
print("There are " + str(Mc.number_served) + " people eating here.")

输出

There are 0 people eating here.
There are 100 people eating here.
There are 12 people eating here.

9-5 尝试登录次数

class User():
	def __init__(self,first_name,last_name,age,sex):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.sex = sex
		self.login_attempts = 0

	def describe_user(self):
		print("first_name : " + self. first_name 
			+ "\nlast_name : " + self.last_name
			+ "\nage : " + self.age
			+ "\nsex : " + self.sex )

	def greet_user(self):
		print("Hello, " + self.first_name + " " + self.last_name + ".")

	def reset_login_attempts(self):
		self.login_attempts = 0

	def increment_login_attempts(self):
		self.login_attempts += 1

HS = User("Sheng", "Huang", "19", "male")
HS.increment_login_attempts()
print(str(HS.login_attempts))
HS.increment_login_attempts()
print(str(HS.login_attempts))
HS.increment_login_attempts()
print(str(HS.login_attempts))
HS.reset_login_attempts()
print(str(HS.login_attempts))

输出

1
2
3
0






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值