파이썬 프로그래밍 제9장 부분 수업 후 연습문제
코드:
# 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 + "
"
message += "The type of cuisine is: " + self.cuisine_type + "
"
message += "Today it has served " + str(self.number_served) + "
"
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() + '
'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '
'
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 + "
"
message += "The type of cuisine is: " + self.cuisine_type + "
"
message += "Today it has served " + str(self.number_served) + "
"
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
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() + '
'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '
'
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() + '
'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '
'
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.