《Python程式設計》第九章部分課後練習題
#9-4 就餐人數:
程式碼:
# 9-4 就餐人數
class Restaurant():
"""define one restaurant class"""
def __init__(self, name, cuisine_type):
self.restaurant_name = name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
message = "This restaurant's name is: " + self.restaurant_name + "\n"
message += "The type of cuisine is: " + self.cuisine_type + "\n"
message += "Today it has served " + str(self.number_served) + "\n"
print(message)
def open_restaurant(self):
message = "The restuarant is running "
print(message)
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self):
self.number_served += 1
restaurant = Restaurant('China Town', 'Chinese cuisine')
restaurant.describe_restaurant()
restaurant.open_restaurant()
restaurant.set_number_served(666)
restaurant.describe_restaurant()
restaurant.increment_number_served()
restaurant.describe_restaurant()
輸出:
This restaurant's name is: China Town
The type of cuisine is: Chinese cuisine
Today it has served 0
The restuarant is running
This restaurant's name is: China Town
The type of cuisine is: Chinese cuisine
Today it has served 666
This restaurant's name is: China Town
The type of cuisine is: Chinese cuisine
Today it has served 667
#9-5 嘗試登入次數:
程式碼:
#9-5 嘗試登入次數
class User():
"""define a class called User, including name and other informations"""
def __init__(self, first_name, last_name, login_attempts, **info):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
self.info = info
def describe_user(self):
message = "The user's name is " + self.first_name.title() + " " + self.last_name.title() + '\n'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '\n'
print(message)
def greet_user(self):
message = "Hello, Dear " + self.first_name.title() + " " + self.last_name.title()
print(message)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
user = User("kobe", "branyt", 10, country = 'america', age = '38')
user.describe_user()
user.greet_user()
print(user.login_attempts)
for a in range(666):
user.increment_login_attempts()
print(user.login_attempts)
user.reset_login_attempts()
print(user.login_attempts)
輸出:
The user's name is Kobe Branyt
Country: America
Age: 38
Hello, Dear Kobe Branyt
10
676
0
#9-6 冰淇淋小店:
程式碼:
#9-6 冰淇淋小店
class Restaurant():
"""define one restaurant class"""
def __init__(self, name, cuisine_type):
self.restaurant_name = name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
message = "This restaurant's name is: " + self.restaurant_name + "\n"
message += "The type of cuisine is: " + self.cuisine_type + "\n"
message += "Today it has served " + str(self.number_served) + "\n"
print(message)
def open_restaurant(self):
message = "The restuarant is running\n"
print(message)
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self):
self.number_served += 1
class IceCreamStand(Restaurant):
"""inherit the class Restaurant"""
def __init__(self, name, cuisine_type, flavors):
super().__init__(name, cuisine_type)
self.flavors = flavors
def show_IceCream_flavors(self):
message = self.restaurant_name + " have different " + self.cuisine_type + ": "
print(message)
for value in self.flavors:
print(value)
flavors = ['Coffee and Cookie ice cream', 'Chocolate ice cream', 'Cherry ice cream']
Stand = IceCreamStand('Ice Cream Stand', 'Ice Cream', flavors)
Stand.describe_restaurant()
Stand.open_restaurant()
Stand.show_IceCream_flavors()
輸出:
This restaurant's name is: Ice Cream Stand
The type of cuisine is: Ice Cream
Today it has served 0
The restuarant is running
Ice Cream Stand have different Ice Cream:
Coffee and Cookie ice cream
Chocolate ice cream
Cherry ice cream
#9-7 管理員:
程式碼:
#9-7 管理員
class User():
"""define a class called User, including name and other informations"""
def __init__(self, first_name, last_name, login_attempts, **info):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
self.info = info
def describe_user(self):
message = "The user's name is " + self.first_name.title() + " " + self.last_name.title() + '\n'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '\n'
print(message)
def greet_user(self):
message = "Hello, Dear " + self.first_name.title() + " " + self.last_name.title()
print(message)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
"""define a special user, Admin"""
def __init__(self, first_name, last_name, login_attempts, privileges, **info):
super().__init__(first_name, last_name, login_attempts, **info)
self.privileges = privileges
def show_privileges(self):
message = "You " + self.privileges
print(message)
admin = Admin("kobe", "branyt", 10, "can do everything", country = 'america', age = '38')
admin.describe_user()
admin.greet_user()
admin.show_privileges()
輸出:
The user's name is Kobe Branyt
Country: America
Age: 38
Hello, Dear Kobe Branyt
You can do everything
#9-8 許可權:
程式碼:
#9-8 許可權
class User():
"""define a class called User, including name and other informations"""
def __init__(self, first_name, last_name, login_attempts, **info):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
self.info = info
def describe_user(self):
message = "The user's name is " + self.first_name.title() + " " + self.last_name.title() + '\n'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '\n'
print(message)
def greet_user(self):
message = "Hello, Dear " + self.first_name.title() + " " + self.last_name.title()
print(message)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
"""define a special user, Admin"""
def __init__(self, first_name, last_name, login_attempts, privileges, **info):
super().__init__(first_name, last_name, login_attempts, **info)
self.privileges = Privileges(privileges)
def show_privileges(self):
message = "You " + self.privileges.privileges
print(message)
class Privileges():
"""define the property of Admin"""
def __init__(self, privileges):
self.privileges = privileges
admin = Admin("kobe", "branyt", 10, "can do everything", country = 'america', age = '38')
admin.describe_user()
admin.greet_user()
admin.show_privileges()
輸出:
The user's name is Kobe Branyt
Country: America
Age: 38
Hello, Dear Kobe Branyt
You can do everything
相關文章
- 《Python程式設計》第十一章部分課後練習題Python程式設計
- 《Python程式設計》第七章部分課後練習題Python程式設計
- 《Python程式設計》第十章部分課後練習題Python程式設計
- 《Python程式設計》第八章部分課後練習題Python程式設計
- Python快速程式設計入門課後程式題答案Python程式設計
- python課後習題Python
- 課後練習
- 第二章課後練習題
- 《Python程式設計練習與解答》之程式設計概論Python程式設計
- 慕課網Python入門練習題---Python
- C primer plus 第六版 第九章 第一題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第二題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第三題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第四題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第五題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第六題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第七題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第八題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第九題 程式設計練習答案程式設計
- C primer plus 第六版 第九章 第十題 程式設計練習答案程式設計
- 程式設計練習程式設計
- Python程式設計基礎練習——撲克牌發牌問題Python程式設計
- C primer plus 第六版 第九章 第十一題 程式設計練習答案程式設計
- 《Java語言程式設計(基礎篇)(原書第10版)》第2~4章部分程式設計練習題程式碼Java程式設計
- 大一C語言程式設計練習題C語言程式設計
- python第七章課後習題Python
- 問題 1011: C語言程式設計教程(第三版)課後習題6.1C語言程式設計
- 1097: C語言程式設計教程(第三版)課後習題10.4C語言程式設計
- Java程式設計練習_241206Java程式設計
- python 實現課堂練習Python
- python第四章課後習題Python
- python第三章課後習題Python
- python第八章課後習題Python
- Python 練習題Python
- Java程式設計(2021春)——第二章課後題(選擇題+程式設計題)答案與詳解Java程式設計
- 新手練習:Python練習題目Python
- 溫度轉換——MOOC《Python語言程式設計》第1周練習題2Python程式設計
- C Primer Plus 第六版 程式設計練習第九章答案 最新出爐程式設計