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("It's a " + self.cuisine_type + " restaurant,and it's name is " + self.restaurant_name)
def open_restaurant(self):
print("The restaurant is open.")
a = Restaurant('LongRiver',"China",)
b = Restaurant("GaliGali","India",)
print(a.restaurant_name)
print(a.cuisine_type)
print(b.restaurant_name)
print(b.cuisine_type)
a.describe_restaurant()
a.open_restaurant()
b.describe_restaurant()
b.open_restaurant()
9.2
class Restaurant:
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("It's a " + self.cuisine_type + " restaurant,and it's name is " + self.restaurant_name)
def open_restaurant(self):
print("The restaurant is open.")
a = Restaurant('LongRiver',"China")
b = Restaurant('GaliGali',"India")
c = Restaurant('Lamenta',"France")
a.describe_restaurant()
b.describe_restaurant()
c.describe_restaurant()
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("It's a " + self.cuisine_type + " restaurant,and it's name is " + self.restaurant_name)
def open_restaurant(self):
print("The restaurant is open.")
def set_number_served(self,num):
self.number_served = num
def increment_number_served(self,num):
self.number_served += num
a = Restaurant('LongRiver',"China")
print(a.number_served)
a.number_served = 100
print(a.number_served)
a.set_number_served(200)
print(a.number_served)
a.increment_number_served(100)
print(a.number_served)
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("It's a " + self.cuisine_type + " restaurant,and it's name is " + self.restaurant_name)
def open_restaurant(self):
print("The restaurant is open.")
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_type,flavors):
super().__init__(restaurant_name,cuisine_type)
self.flavors = flavors
def show_flavors(self):
print(self.flavors)
a = IceCreamStand("shalala","Ice",['Sundae','Popsicle','Mango','Cherry'])
a.show_flavors()
============================================================================
10.1
with open('test.txt') as file_object:
contents = file_object.read()
print(contents)
with open('test.txt') as file_object:
for line in file_object:
print(line.rstrip())
with open('test.txt') as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
10.2
with open('test.txt') as file_object:
lines = file_object.readlines()
for line in lines:
oline = line.replace('Python','C')
print(oline.rstrip())
10.3
name = input('please enter your name:')
with open('guest.txt','a') as file_object:
file_object.write(name + '\n')
10.4
name = input('please enter your name:')
while name != ' ':
print(name + ",WELCOME!")
with open('guest_book.txt','a') as file_object:
file_object.write(name + '\n')
name = input('please enter your name:')
10.5
reason = input('why you like programming?:')
while reason != ' ':
with open('reason.txt','a') as file_object:
file_object.write(reason + '\n')
reason = input('why you like programming?:')
10.6
print("input two num:")
a = input("first:")
b = input("second:")
try:
ans = int(b) + int(a)
except ValueError:
print("please enter number!")
else:
print(ans)
10.7
while True:
print("input two num:")
a = input("first:")
b = input("second:")
try:
ans = int(b) + int(a)
except ValueError:
print("please enter number!try again.")
else:
print(ans)
break
10.8
import json
num = input("please enter your favorite num:")
with open('num.json', 'w') as f_obj:
json.dump(num,f_obj)
with open('num.json') as f_obj:
num = json.load(f_obj)
print("I know your favorite number!It's " + num)