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 this restaurant is: " + self.restaurant_name, end = '')
print(', and the cuisine type is ' + self.cuisine_type)
def open_restaurant(self):
print("This restaurant is open!")
my_restaurant = Restaurant("Perfect Restaurant", "Chinese food")
his_restaurant = Restaurant("Italian Restaurant", "Itlian food")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
his_restaurant.describe_restaurant()
his_restaurant.open_restaurant()
结果如下:
9-4
class Restaurant():
def __init__(self, restaurant_name, cuisine_type, number_served = 0):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = number_served
def describe_restaurant(self):
print("The name of this restaurant is: " + self.restaurant_name, end = '')
print(', and the cuisine type is ' + self.cuisine_type)
def open_restaurant(self):
print("This restaurant is open!")
def set_number_served(self, value):
self.number_served = value
print(self.number_served)
def increment_number_served(self):
self.number_served += 1
my_restaurant = Restaurant("Perfect Restaurant", "Chinese food")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
my_restaurant.set_number_served(15)
#下面将就餐人数设为35
for i in range(20):
my_restaurant.increment_number_served()
print(my_restaurant.number_served)
结果如下:
9-6
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type, number_served = 0):
super().__init__(restaurant_name, cuisine_type, number_served)
self.flavors = ["apple", "orange", "pineapple", "vanilla", "strawberry"]
def printFlavors(self):
print(self.flavors)
IceCreamStand0 = IceCreamStand("Ice", "Ice-cream")
IceCreamStand0.printFlavors()
输出为:
['apple', 'orange', 'pineapple', 'vanilla', 'strawberry']
9-10
from restaurant import Restaurant
my_restaurant = Restaurant("Perfect Restaurant", "Chinese food")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
输出为:
The name of this restaurant is: Perfect Restaurant, and the cuisine type is Chinese food
This restaurant is open!
10-1
In Python you can calculate;
In Python you can make games;
In Python you can do many things.
In Python you can calculate;
In Python you can make games;
In Python you can do many things.
In Python you can calculate;
输出结果为:
with open("learning_python.txt") as file_object:
contents = file_object.read()
print(contents)
with open("learning_python.txt") as file_object:
for line in file_object:
print(line.rstrip())
with open("learning_python.txt") as file_object:
lines = file_object.readline()
print(lines)
10-3
with open("guest.txt", 'w') as file_object:
name = input("Please input your name:")
file_object.write(name)
10-6
a = input("Please input a number:")
b = input("Please input a number:")
try:
c = int(a) + int(b)
except ValueError:
print("You shoule input number but not words!")
else:
print(c)
结果为: