Question
教材中课后的练习,9-1到9-15,选一些写到你的博客上
Answer
'''
SYSU - [专选]高级编程技术 - Week5, Lecture1 HW
by Duan
2018.04.02
'''
'''
9-1 餐馆:
'''
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("Restaurant name is:", self.restaurant_name)
print('Cuisine type is:', self.cuisine_type)
def open_restaurant(self):
print('Our restaurant is opening now!')
restaurant = Restaurant('Starbucks', 'coffee')
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
#Output:
# Starbucks
# coffee
# Restaurant name is: Starbucks
# Cuisine type is: coffee
# Our restaurant is opening now!
'''
9-3 用户:
'''
class User():
def __init__(self, first_name, last_name, birthday):
self.first_name = first_name
self.last_name = last_name
self.birthday = birthday
def describe_user(self):
print('User name:', self.first_name, self.last_name)
print('Birthday:', self.birthday)
def greet_user(self):
print("It's", self.birthday, 'today. \nHappy birthday,',
self.first_name, self.last_name + "!")
print('------------ case 1 ------------')
Peter = User('Peter', 'Parker', '8.22') # 随便写的生日
Peter.describe_user()
Peter.greet_user()
print('------------ case 2 ------------')
Steven = User('Steven', 'Rogers', '7.4')
Steven.describe_user()
Steven.greet_user()
print('------------ case 3 ------------')
Bucky = User('James', 'Barnes', '12.30')
Bucky.describe_user()
Bucky.greet_user()
#Output:
# ------------ case 1 ------------
# User name: Peter Parker
# Birthday: 8.22
# It's 8.22 today.
# Happy birthday, Peter Parker!
# ------------ case 2 ------------
# User name: Steven Rogers
# Birthday: 7.4
# It's 7.4 today.
# Happy birthday, Steven Rogers!
# ------------ case 3 ------------
# User name: James Barnes
# Birthday: 12.30
# It's 12.30 today.
# Happy birthday, James Barnes!
'''
9-5 尝试登陆次数:
'''
class User():
def __init__(self, first_name, last_name, birthday):
self.first_name = first_name
self.last_name = last_name
self.birthday = birthday
self.login_attempts = 0;
def describe_user(self):
print('User name:', self.first_name, self.last_name)
print('Birthday:', self.birthday)
def greet_user(self):
print("It's", self.birthday, 'today. \nHappy birthday,',
self.first_name, self.last_name + "!")
def increment_login_attempts(self):
self.login_attempts = self.login_attempts + 1
def reset_login_attempts(self):
self.login_attempts = 0;
user = User('Jacie', 'Chen', '4.7')
print(user.login_attempts)
for i in range(1, 5):
user.increment_login_attempts()
print(user.login_attempts)
user.reset_login_attempts();
print(user.login_attempts)
#Output:
# 0
# 1
# 2
# 3
# 4
# 0
'''
9-6 冰淇淋小店
'''
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("Restaurant name is:", self.restaurant_name)
print('Cuisine type is:', self.cuisine_type)
def open_restaurant(self):
print('Our restaurant is opening now!')
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, *flavors):
super().__init__(restaurant_name, 'snacks')
self.flavors = flavors
def print_icecream(self):
print('IceCreams:')
for flavor in self.flavors:
print(flavor.title(), 'IceCream')
icecream = IceCreamStand('DQ', 'Chocolate', 'matcha', 'oreo')
icecream.print_icecream()
#Output:
# IceCreams:
# Chocolate IceCream
# Matcha IceCream
# Oreo IceCream
'''
9-10 导入 Restaurant 类:
'''
'''
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("Restaurant name is:", self.restaurant_name)
print('Cuisine type is:', self.cuisine_type)
def open_restaurant(self):
print('Our restaurant is opening now!')
'''
from restaurant import Restaurant
restaurant = Restaurant("McDonald's", 'fast food')
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
#Output:
# McDonald's
# fast food
# Restaurant name is: McDonald's
# Cuisine type is: fast food
# Our restaurant is opening now!
'''
9-14 骰子:
'''
from random import randint
class Die():
def __init__(self):
self.sides = 6;
def roll_die(self):
self.sides = randint(1, 6)
print(self.sides)
die = Die()
for i in range(1, 11):
if i == 1: postfix = 'st'
elif i == 2: postfix = 'nd'
elif i == 3: postfix = 'rd'
else: postfix = 'th'
print('The', str(i) + postfix, 'roll:', end = '')
die.roll_die()
#Output:
# The 1st roll:1
# The 2nd roll:6
# The 3rd roll:2
# The 4th roll:4
# The 5th roll:3
# The 6th roll:6
# The 7th roll:3
# The 8th roll:1
# The 9th roll:2
# The 10th roll:5