파이썬 프로그래밍 제9장 부분 수업 후 연습문제

8006 단어
#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 + "
" 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

좋은 웹페이지 즐겨찾기