【Python Homework】Chapter09 类

9-1 餐馆 :创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名为describe_restaurant() 的方法和一个名为open_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(self.restaurant_name)
		print(self.cuisine_type)
	
	def open_restaurant(self):
		print('The restaurant is opening.')

my_restaurant = Restaurant('Mcdonald','fast food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()



9-3 用户 :创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。    创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。
class User:
	def __init__(self, first_name, last_name, age):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		
	def describe_user(self):
		print('name: ' + self.first_name + ' ' + self.last_name)
		print('age: ' + str(self.age))
		
	def greet_user(self):
		print('Hello! ' + self.first_name + ' ' + self.last_name)
	
user = User('Thomas', 'Muller', 28)
user.describe_user()
user.greet_user()




9-5尝试登录次数:在为完成练习9-3而编写的User类中,添加一个名为login_attempts的属性。编写一个名为 increment_ login_attempts() 的方法,它将属性login_attempts 的值加1。再编写一个名为reset_login_attempts() 的方法,它将属性login _attempts 的值重置为0。
根据User 类创建一个实例,再调用方法increment_login_attempts() 多次。打印属性login_attempts 的值,确认它被正确地递增;然后,调用方法reset_login_attempts() ,并再次打印属性login_attempts 的值,确认它被重置为0。

class User:
	def __init__(self, first_name, last_name, age, login_attempts):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.login_attempts = login_attempts
		
	def describe_user(self):
		print('name: ' + self.first_name + ' ' + self.last_name)
		print('age: ' + str(self.age))
		
	def greet_user(self):
		print('Hello! ' + self.first_name + ' ' + self.last_name)
		
	def increment_login_attempts(self):
		self.login_attempts += 1
		return self.login_attempts
		
	def reset_login_attempts(self):
		self.login_attempts = 0
		return self.login_attempts
	
user = User('Thomas', 'Muller', 28,0)
print(user.increment_login_attempts())
print(user.increment_login_attempts())
print(user.increment_login_attempts())
print(user.reset_login_attempts())




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

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

	def show_flavors(self):
		print(self.flavors)
		
creams = IceCreamStand('ice room', 'ice-cream store', ['milk ice-cream', 'fruit ice-cream', 'tea ice-cream'])
creams.show_flavors()




9-9 电瓶升级 :在本节最后一个electric_car.py版本中,给Battery 类添加一个名为upgrade_battery() 的方法。这个方法检查电瓶容量,如果它不是85,就将它设置为85。创建一辆电瓶容量为默认值的电动汽车,调用方法get_range() ,然后对电瓶进行升级,并再次调用get_range() 。你会看到这辆汽车的续航里程增加了。
class Car():
	def __init__(self, make, model, year):
		self.make = make
		self.model = model
		self.year = year
		self.odometer_reading = 0

class Battery():
	def __init__(self, battery_size=70):
		self.battery_size = battery_size
	def describe_battery(self):
		print("This car has a " + str(self.battery_size) + "-kWh battery.")
	def get_range(self):
		if self.battery_size == 70:
			range = 240
		elif self.battery_size == 85:
			range = 270
		message = "This car can go approximately " + str(range)
		message += " miles on a full charge."
		print(message)
	def upgrade_battery(self):
		if self.battery_size != 85:
			self.battery_size = 85

class ElectricCar(Car):
	def __init__(self, make, model, year):
		super().__init__(make, model, year)
		self.battery = Battery()
		
my_tesla = ElectricCar('tesla', 'model s', 2016)
my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()

9-10 导入Restaurant 类 :将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调用Restaurant 的一个方法,以确认import 语句正确无误。
# restaurant.py :
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)
		print(self.cuisine_type)
	
	def open_restaurant(self):
		print('The restaurant is opening.')
from restaurant import Restaurant

my_reataurant = Restaurant('ice room', 'ice-cream store')
my_reataurant.describe_restaurant()


9-14 骰子 :模块random 包含以各种方式生成随机数的函数,其中的randint() 返回一个位于指定范围内的整数,例如,下面的代码返回一个1~6内的整数:
    from random import randint
    x = randint(1, 6)
请创建一个Die 类,它包含一个名为sides 的属性,该属性的默认值为6。编写一个名为roll_die() 的方法,它打印位于1和骰子面数之间的随机数。创建一个6面的骰子,再掷10次。 创建一个10面的骰子和一个20面的骰子,并将它们都掷10次。
class Die:
	def __init__(self, sides):
		self.sides = sides
	
	def roll_die(self):
		i=0
		while i < 10:
			from random import randint
			x = randint(1, self.sides)
			print(x)
			i = i+1
		print()
			
die1 = Die(6)
die2 = Die(10)
die3 = Die(20)
die1.roll_die()
die2.roll_die()
die3.roll_die()



2017.04.02 [09]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值