1. 程式人生 > >Python程式設計入門到實踐Chapter-09

Python程式設計入門到實踐Chapter-09

"""
Exercise 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("The name of restaurant is %s." %self.restaurant_name)
        print("The type of cuisine is %s." %self.cuisine_type)

    def open_restaurant(self):
        print("Our restaurant is open now.")

my_restaurant = Restaurant('ChJp', 'Cfood')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
"""
Exercise 9-3:
使用者

"""

class User():
    def __init__(self, first_name, last_name, number):
        self.first_name = first_name
        self.last_name = last_name
        self.number = number

    def describe_user(self):
        print("The information of the user are as follow:")
        print("Name: %s %s" %(self.first_name.title(),
                              self.last_name.title()))
        print("Number: %d" %self.number)

    def greet_user(self):
        print("Welcome! Deer %s %s"%(self.first_name.title(),
                              self.last_name.title()))

user1 = User('dylan', 'smith', 21019828)
user2 = User('Jack', 'Michael', 20192128)

user1.greet_user()
user1.describe_user()

print('')
user2.greet_user()
user2.describe_user()
"""
Exercise 9-4:
就餐人數

"""

class Restaurant():

    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        print("The name of restaurant is %s." % self.restaurant_name)
        print("The type of cuisine is %s." % self.cuisine_type)

    def updat_served(self, served):
        self.number_served = served

    def open_restaurant(self):
        print("Our restaurant have served %d persons." %self.number_served)

my_restaurant = Restaurant('ChJp', 'Cfood')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
my_restaurant.updat_served(20)
my_restaurant.open_restaurant()
"""
Exercise 9-5:
嘗試登陸次數

"""

class User():
    def __init__(self, first_name, last_name, number):
        self.first_name = first_name
        self.last_name = last_name
        self.number = number
        self.login_attempts = 0

    def describe_user(self):
        print("The information of the user are as follow:")
        print("Name: %s %s" %(self.first_name.title(),
                              self.last_name.title()))
        print("Number: %d" %self.number)
        print("the times of Login: %d" %self.login_attempts)

    def greet_user(self):
        print("Welcome! Deer %s %s"%(self.first_name.title(),
                              self.last_name.title()))

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0

user1 = User('dylan', 'smith', 21019828)

user1.greet_user()
user1.describe_user()
user1.increment_login_attempts()
print('')
user1.describe_user()
user1.reset_login_attempts()
print('')
user1.describe_user()
"""
Exercise 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("The name of restaurant is %s." %self.restaurant_name)
        print("The type of cuisine is %s." %self.cuisine_type)

    def open_restaurant(self):
        print("Our restaurant is open now.")

class IceCreamStand(Restaurant,):

    def __init__(self, restaurant_name, cuisine_type, flavors):
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = flavors

    def describe_flavors(self):
        print("About flavors:")
        for flavor in self.flavors:
            print(flavor)

flavors = ['a', 'b', 'c']
ice_cream_stand = IceCreamStand('ChJp', 'Cfood', flavors)
ice_cream_stand.describe_restaurant()
ice_cream_stand.describe_flavors()
"""
Exercise 9-7:
管理員

"""

class User():
    def __init__(self, first_name, last_name, number):
        self.first_name = first_name
        self.last_name = last_name
        self.number = number

    def describe_user(self):
        print("The information of the user are as follow:")
        print("Name: %s %s" %(self.first_name.title(),
                              self.last_name.title()))
        print("Number: %d" %self.number)

    def greet_user(self):
        print("Welcome! Deer %s %s"%(self.first_name.title(),
                              self.last_name.title()))

class Admin(User):
    def __init__(self, first_name, last_name, number):
        super().__init__(first_name, last_name, number)
        self.privileges = ['can add post',
                           'can delete post',
                           'can ban user']

    def show_privileges(self):
        print("If you are a administrator, you can do:")
        for privilege in self.privileges:
            print(privilege)

ad = Admin('dylan', 'smith', 2010)
ad.show_privileges()
"""
Exercise 9-8:
許可權

"""

class Privileges():
    def __init__(self):
        self.privileges = ['can add post',
                           'can delete post',
                           'can ban user']

    def show_privileges(self):
        print("If you are a administrator, you can do:")
        for privilege in self.privileges:
            print(privilege)

class User():
    def __init__(self, first_name, last_name, number):
        self.first_name = first_name
        self.last_name = last_name
        self.number = number

    def describe_user(self):
        print("The information of the user are as follow:")
        print("Name: %s %s" %(self.first_name.title(),
                              self.last_name.title()))
        print("Number: %d" %self.number)

    def greet_user(self):
        print("Welcome! Deer %s %s"%(self.first_name.title(),
                              self.last_name.title()))

class Admin(User):
    def __init__(self, first_name, last_name, number):
        super().__init__(first_name, last_name, number)
        self.privileges = Privileges() # 類做為屬性

ad = Admin('dylan', 'smith', 2010)
ad.privileges.show_privileges()
"""
Exercise 9-9:
電瓶升級

"""

class Car():
    def __init__(self, make, model, year):
        self.make = make
        self.model =model
        self.year = year
        self.odometer_reading = 0

    def print_descriptive_name(self):
        print("%d %s %s" %(self.year, self.make,self.model))

    def print_odometer(self):
        print("This car has %d miles on it." %self.odometer_reading)

    def update_odometer(self, mileage):
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        self.odometer_reading += miles

class Battery():
    def __init__(self, battery_size=70):
        self.battery_size = battery_size

    def describe_battery(self):
        print("This car has a %d -kwh battery." %self.battery_size)

    def update_battery(self):
        if self.battery_size != 85:
            self.battery_size = 85

    def ger_range(self):
        if self.battery_size == 70:
            range = 240
        elif self.battery_size == 85:
            range = 270
        print("This car can go approximately %d miles on a full charge." %range)

class ElectricCar(Car):
    def __init__(self, make, model, year):
        super().__init__(make,model,year)
        self.battery = Battery()

my_car = ElectricCar('Tesla', 'Model-S', 2010)
my_car.print_descriptive_name()
my_car.battery.update_battery()
my_car.battery.describe_battery()
my_car.battery.ger_range()
"""
Exercise 9-10:
匯入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("The name of restaurant is %s." %self.restaurant_name)
        print("The type of cuisine is %s." %self.cuisine_type)

    def open_restaurant(self):
        print("Our restaurant is open now.")
        
        
# 另外一個模組中匯入
'''
from python入門到實踐.chapter_09.restaurant import Restaurant

my_restaurant = Restaurant('ChJp', 'Cfood')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()    
'''   
"""
Exercise 9-11:
匯入Admin類

"""
class Privileges():
    def __init__(self):
        self.privileges = ['can add post',
                           'can delete post',
                           'can ban user']

    def show_privileges(self):
        print("If you are a administrator, you can do:")
        for privilege in self.privileges:
            print(privilege)

class User():
    def __init__(self, first_name, last_name, number):
        self.first_name = first_name
        self.last_name = last_name
        self.number = number

    def describe_user(self):
        print("The information of the user are as follow:")
        print("Name: %s %s" %(self.first_name.title(),
                              self.last_name.title()))
        print("Number: %d" %self.number)

    def greet_user(self):
        print("Welcome! Deer %s %s"%(self.first_name.title(),
                              self.last_name.title()))

class Admin(User):
    def __init__(self, first_name, last_name, number):
        super().__init__(first_name, last_name, number)
        self.privileges = Privileges() # 類做為屬性
        
# 另外一個模組中匯入
'''
from python入門到實踐.chapter_09.admin import Admin,User,Privileges

ad = Admin('dylan', 'smith', 2010)
ad.privileges.show_privileges()
'''
"""
Exercise 9-12:
多個模組

"""

from python入門到實踐.chapter_09.user import User
from python入門到實踐.chapter_09.privileges_admin \
    import Privileges, Admin

ad = Admin('dylan', 'smith', 2010)
ad.privileges.show_privileges()

# 模組user中
'''
class User():
    def __init__(self, first_name, last_name, number):
        self.first_name = first_name
        self.last_name = last_name
        self.number = number

    def describe_user(self):
        print("The information of the user are as follow:")
        print("Name: %s %s" %(self.first_name.title(),
                              self.last_name.title()))
        print("Number: %d" %self.number)

    def greet_user(self):
        print("Welcome! Deer %s %s"%(self.first_name.title(),
                              self.last_name.title()))
'''

# 模組privileges_admin中
'''
from python入門到實踐.chapter_09.user import User

class Privileges():
    def __init__(self):
        self.privileges = ['can add post',
                           'can delete post',
                           'can ban user']

    def show_privileges(self):
        print("If you are a administrator, you can do:")
        for privilege in self.privileges:
            print(privilege)

class Admin(User):
    def __init__(self, first_name, last_name, number):
        super().__init__(first_name, last_name, number)
        self.privileges = Privileges() # 類做為屬性
'''
"""
Exercise 9-13:
使用OrderedDict()

"""

from collections import OrderedDict

# 建立一個空的有序字典
glossary = OrderedDict() 

glossary['string'] = 'A series of characters.'
glossary['comment'] = 'A note in a program that the Python interpreter ignores.'
glossary['list'] = 'Work through a collection of items, one at a time.'
glossary['dictionary'] = "A collection of key-value pairs."

for key, value in glossary.items():
    print("%s : %s" %(key.title(), value.title()))

glossary['float'] = 'A numerical value with a decimal component.'
glossary['conditional test'] = 'A comparison between two values.'
print('')
for key, value in glossary.items():
    print("%s : %s" %(key.title(), value.title()))
"""
Exercise 9-14:
骰子

"""

from random import randint

class Die():
    '''模擬骰子'''
    def __init__(self, sides=6):
        self.sides = sides

    def roll_die(self):
        result = randint(1,6) # 返回一個1-6的整數
        print(result)

die = Die()

# 投擲10次
for n in range(10):
    die.roll_die()