《Python從入門到實踐》第九章動手試一試


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(self.restaurant_name + "菜館開業大酬賓,啤酒買二送一")

chuancai = Restaurant("小四川", "水煮魚")
chuancai.describe_restaurant()
chuancai.open_restaurant()

9-2 三家餐館 :根據你為完成練習9-1而編寫的類創建三個實例,並對每個實例調用方法describe_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(self.restaurant_name + "菜館開業大酬賓,啤酒買二送一")

chuancai = Restaurant("小四川", "川菜")
chuancai.describe_restaurant()
chuancai.open_restaurant()

yuecai = Restaurant("太白居", "粵菜")
yuecai.describe_restaurant()
yuecai.open_restaurant()

xican = Restaurant("KC", "快餐")
xican.describe_restaurant()
xican.open_restaurant()

9-3 用戶 :創建一個名為User 的類,其中包含屬性first_name 和last_name ,還有用戶簡介通常會存儲的其他幾個屬性。在類User 中定義一個名為describe_user() 的方法,它打印用戶信息摘要;再定義一個名為greet_user() 的方法,它向用戶發出個性化的問候。

class User():

	def __init__(self, first_name, last_name, age, city):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.city = city
	def describe_user(self):
		print("用戶名:" + self.first_name + self.last_name)
		print("年齡: " + str(self.age))
		print("居住城市: " + self.city)
		
	def greet_user(self):
		print("Hello, " + self.first_name + self.last_name)

user1 = User("z", "sh",17, 'zhongguo')
user1.describe_user()
user1.greet_user()

9-4 就餐人數 :在為完成練習9-1而編寫的程序中,添加一個名為number_served 的屬性,並將其默認值設置為0。根據這個類創建一個名為restaurant 的實例;打印有多少人在這家餐館就餐過,然后修改這個值並再次打印它。

  • 添加一個名為set_number_served() 的方法,它讓你能夠設置就餐人數。調用這個方法並向它傳遞一個值,然后再次打印這個值。
  • 添加一個名為increment_number_served() 的方法,它讓你能夠將就餐人數遞增。調用這個方法並向它傳遞一個這樣的值:你認為這家餐館每天可能接待的就餐人數。

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。

9-6 冰淇淋小店 :冰淇淋小店是一種特殊的餐館。編寫一個名為IceCreamStand 的類,讓它繼承你為完成練習9-1或練習9-4而編寫的Restaurant 類。這兩個版 本的Restaurant 類都可以,挑選你更喜歡的那個即可。添加一個名為flavors 的屬性,用於存儲一個由各種口味的冰淇淋組成的列表。編寫一個顯示這些冰淇淋 的方法。創建一個IceCreamStand 實例,並調用這個方法。

9-7 管理員 :管理員是一種特殊的用戶。編寫一個名為Admin 的類,讓它繼承你為完成練習9-3或練習9-5而編寫的User 類。添加一個名為privileges 的屬性,用 於存儲一個由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)組成的列表。編寫一個名為show_privileges() 的方法,它 顯示管理員的權限。創建一個Admin 實例,並調用這個方法。

9-8 權限 :編寫一個名為Privileges 的類,它只有一個屬性——privileges ,其中存儲了練習9-7 所說的字符串列表。將方法show_privileges() 移到這 個類中。在Admin 類中,將一個Privileges 實例用作其屬性。創建一個Admin 實例,並使用方法show_privileges() 來顯示其權限。

9-9 電瓶升級 :在本節最后一個electric_car.py版本中,給Battery 類添加一個名為upgrade_battery() 的方法。這個方法檢查電瓶容量,如果它不是85,就將它 設置為85。創建一輛電瓶容量為默認值的電動汽車,調用方法get_range() ,然后對電瓶進行升級,並再次調用get_range() 。你會看到這輛汽車的續航里程增 加了。

9-10 導入Restaurant 類 :將最新的Restaurant 類存儲在一個模塊中。在另一個文件中,導入Restaurant 類,創建一個Restaurant 實例,並調 用Restaurant 的一個方法,以確認import 語句正確無誤。

9-11 導入Admin 類 :以為完成練習9-8而做的工作為基礎,將User 、Privileges 和Admin 類存儲在一個模塊中,再創建一個文件,在其中創建一個Admin 實例 並對其調用方法show_privileges() ,以確認一切都能正確地運行。

9-12 多個模塊 :將User 類存儲在一個模塊中,並將Privileges 和Admin 類存儲在另一個模塊中。再創建一個文件,在其中創建一個Admin 實例,並對其調用方 法show_privileges() ,以確認一切都依然能夠正確地運行。

9-13 使用OrderedDict :在練習6-4中,你使用了一個標准字典來表示詞匯表。請使用OrderedDict 類來重寫這個程序,並確認輸出的順序與你在字典中添加鍵 —值對的順序一致。

9-14 骰子 :模塊random 包含以各種方式生成隨機數的函數,其中的randint() 返回一個位於指定范圍內的整數,例如,下面的代碼返回一個1~6內的整數:

from random import randint
x = randint(1, 6)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM