20211102, 20211103
9-1
class Restaurant():
def __init__(self, restaurant_name, cuisine_type): #_init_ (x)
self.name = restaurant_name
self.type = cuisine_type
def describe_restarant(self):
print(self.name + ' ' +self.type)
def open_restaurant(self):
print(self.name + ' is working')
restaurant1 = Restaurant('kfc', 'fastfood')
restaurant1.describe_restarant()
restaurant1.open_restaurant()
9-2
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.name = restaurant_name
self.type = cuisine_type
def describe_restarant(self):
print(self.name.title() + ' is ' +self.type)
def open_restaurant(self):
print(self.name + ' is working')
restaurant1 = Restaurant('kfc', 'fastfood')
restaurant2 = Restaurant('shaxian snack', 'snack')
restaurant3 = Restaurant('peking duck', 'speciality')
restaurant1.describe_restarant()
restaurant2.describe_restarant()
restaurant3.describe_restarant()
9-3
class User():
def __init__(self, first_name, last_name):
self.fname = first_name
self.lname = last_name
def describe(self):
print(self.fname.title() + ' ' + self.lname.title())
print("What's up!\n")
user1 = User('ali', 'suzuki')
user2 = User('ai', 'hashimoto')
user3 = User('ji-eun', 'lee')
user1.describe()
user2.describe()
user3.describe()
9-4
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.name = restaurant_name
self.type = cuisine_type
self.number_served = 0
def describe_restarant(self):
print(self.name + ' ' +self.type)
def open_restaurant(self):
print(self.name + ' is working')
def set_number_server(self, n):
self.number_served = n
def increment_number_server(self, add_num):
self.number_served += add_num
restaurant1 = Restaurant('kfc', 'fastfood')
restaurant1.number_served = 5
restaurant1.describe_restarant()
restaurant1.open_restaurant()
restaurant1.set_number_server(99)
print('There is ' + str(restaurant1.number_served) + ' customer(s).')
restaurant1.increment_number_server(1)
print('There is ' + str(restaurant1.number_served) + ' customer(s).')
restaurant1.increment_number_server(11)
print('There is ' + str(restaurant1.number_served) + ' customer(s).')
9-5
class User():
def __init__(self, first_name, last_name):
self.fname = first_name
self.lname = last_name
self.login_attempts = 0
def describe(self):
print(self.fname.title() + ' ' + self.lname.title())
print("What's up!\n")
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
user1 = User('ali', 'suzuki')
user1.describe()
user1.increment_login_attempts()
print(user1.login_attempts)
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
print(user1.login_attempts)
user1.reset_login_attempts()
print(user1.login_attempts)
9-6
"""chose 9-1"""
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.name = restaurant_name
self.type = cuisine_type
def describe_restarant(self):
print(self.name + ' is ' +self.type)
def open_restaurant(self):
print(self.name + ' is working')
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors = ['strawberry','chocolate']
def show_icecream(self):
print('There are some flavors: ')
for flavor in self.flavors:
print(flavor)
icecream1 = IceCreamStand('kfc', 'fastfood')
icecream1.describe_restarant()
icecream1.open_restaurant()
icecream1.show_icecream()
9-7
"""chose 9-3"""
class User():
def __init__(self, first_name, last_name):
self.fname = first_name
self.lname = last_name
def describe(self):
print(self.fname.title() + ' ' + self.lname.title())
print("What's up!\n")
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = ['can add post', 'can delete post', 'can ba user']
def show_privileges(self):
print('u have rights as following:')
for p in self.privileges:
print(p)
admin1 = Admin('ali', 'suzuki')
admin1.describe()
admin1.show_privileges()
9-8
class User():
def __init__(self, first_name, last_name):
self.fname = first_name
self.lname = last_name
def describe(self):
print(self.fname.title() + ' ' + self.lname.title())
print("What's up!\n")
class Privileges():
def __init__(self,privileges = []): #----------(self,privileges) is wrong
self.privileges = ['can add post', 'can delete post', 'can ba user']
def show_privileges(self):
print('u have rights as following:')
for p in self.privileges:
print(p)
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = Privileges()
admin1 = Admin('ali', 'suzuki')
admin1.describe()
admin1.privileges.show_privileges()
9-9
class Car():
"""A simple attempt to represent a car."""
def __init__(self, manufacturer, model, year):
"""Initialize attributes to describe a car."""
self.manufacturer = manufacturer
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = str(self.year) + ' ' + self.manufacturer + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print("This car has " + str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
"""
Set the odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
class Battery():
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=60):
"""Initialize the batteery's attributes."""
self.battery_size = battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print("This car has a " + str(self.battery_size) + "-kWh battery.")
def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 60:
range = 140
elif self.battery_size == 85:
range = 185
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
def upgrade_battery(self):
if self.battery_size != 85:
self.battery_size = 85
class ElectricCar(Car):
"""Models aspects of a car, specific to electric vehicles."""
def __init__(self, manufacturer, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(manufacturer, model, year)
self.battery = Battery()
ecar = ElectricCar('tesla', 'model s', 2021)
ecar.battery.get_range()
ecar.battery.upgrade_battery()
ecar.battery.get_range()
9-10
restaurant.py
"""chose 9-6"""
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.name = restaurant_name
self.type = cuisine_type
def describe_restarant(self):
print(self.name + ' is ' +self.type)
def open_restaurant(self):
print(self.name + ' is working')
my_restaurant.py
from restaurant import Restaurant #put what u wanna import in the same content
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors = ['strawberry','chocolate']
def show_icecream(self):
print('There are some flavors: ')
for flavor in self.flavors:
print(flavor)
icecream1 = IceCreamStand('kfc', 'fastfood')
icecream1.describe_restarant()
icecream1.open_restaurant()
icecream1.show_icecream()
9-11
user.py
class User():
def __init__(self, first_name, last_name):
self.fname = first_name
self.lname = last_name
def describe(self):
print(self.fname.title() + ' ' + self.lname.title())
print("What's up!\n")
class Privileges():
def __init__(self,privileges = []): #----------(self,privileges) is wrong
self.privileges = ['can add post', 'can delete post', 'can ba user']
def show_privileges(self):
print('u have rights as following:')
for p in self.privileges:
print(p)
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = Privileges()
admin_1.py
from user import Admin
admin1 = Admin('ali', 'suzuki')
admin1.describe()
admin1.privileges.show_privileges()
9-12
admin.py
from user import User
class Privileges():
def __init__(self,privileges = []): #----------(self,privileges) is wrong
self.privileges = ['can add post', 'can delete post', 'can ba user']
def show_privileges(self):
print('u have rights as following:')
for p in self.privileges:
print(p)
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = Privileges()
user.py
class User():
def __init__(self, first_name, last_name):
self.fname = first_name
self.lname = last_name
def describe(self):
print(self.fname.title() + ' ' + self.lname.title())
print("What's up!\n")
admin1.py
from admin import Admin
admin1 = Admin('ali', 'suzuki')
admin1.describe()
admin1.privileges.show_privileges()
9-13
from collections import OrderedDict
glossary = OrderedDict()
glossary['access'] = '6'
glossary['add'] = '7'
glossary['create'] = '8'
glossary['modify'] = '9'
glossary['delete'] = '10'
for term in glossary:
print(term + ': ' + glossary[term])
9-14
from random import randint
class Die():
def __init__(self, sides=6):
self.sides = sides
def roll_die(self):
print(randint(1, self.sides)) # randint(1, n) -> 1, 2, ... , n
die6 = Die(6)
for i in range(10):
die6.roll_die()
print('\n')
die10 = Die(10)
for i in range(10):
die10.roll_die()
print('\n')
die20 = Die(20)
for i in range(10):
die20.roll_die()
Python编程:从入门到实践 练习答案 Chapter09
最新推荐文章于 2024-07-21 22:27:18 发布