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 restaurant name is "+ self.restaurant_name) print("The cuisine_type is " + self.cuisine_type) def open_restaurant(self): print("The restaurant is opening!") restaurant = Restaurant('maidanna','cook') restaurant.describe_restaurant() restaurant.open_restaurant()
the restaurant name is maidanna The cuisine_type is cook The restaurant is opening!
9-4就餐人数:
class Restaurant(): def __init__(self,restaurant_name,cuisine_type,number_serve = 0): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def set_number_served(self,value): self.number_serve = value print("The restaurant can serve "+str(self.number_serve)+".") def increment_number_served(self,value): self.number_serve+=value print("The restaurant now can serve "+str(self.number_serve)+".") def describe_restaurant(self): print("the restaurant name is "+ self.restaurant_name) print("The cuisine_type is " + self.cuisine_type) def open_restaurant(self): print("The restaurant is opening!") restaurant = Restaurant('maidanna','cook') restaurant.describe_restaurant() restaurant.open_restaurant() restaurant.set_number_served(50) restaurant.increment_number_served(30)
the restaurant name is maidanna The cuisine_type is cook The restaurant is opening! The restaurant can serve 50. The restaurant now can serve 80.
9-6冰激凌小店:
class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type class IceRestaurant(Restaurant): def __init__(self,restaurant_name,cuisine_type): super().__init__(restaurant_name,cuisine_type) self.flavors = ['Cheese','Apple','Juice'] def show_flavors(self): print(self.flavors) IceCreamStand = IceRestaurant('Maidanna','cook') IceCreamStand.show_flavors()
['Cheese', 'Apple', 'Juice']
10-1Python学习笔记:
file_name = 'learning_python.txt' print("First:") with open(file_name) as file_object: contents = file_object.read() print(contents) print("Second:") with open(file_name) as file_object: for line in file_object: print(line.rstrip()) print("Third") with open(file_name) as file_object: lines = file_object.readlines() for line in lines: print(line.rstrip())
First: In Python you can do whatever you want. In Python you can try everything. Second: In Python you can do whatever you want. In Python you can try everything. Third In Python you can do whatever you want. In Python you can try everything.
10-3访客:
name = input("Please input your name!") with open('guest.txt','w') as file_object: file_object.write(name)
Please input your name!qianjq
guest.txt文件的内容为:qianjq
temp = "Please input your name!\n" temp += "If you input 'q',then end the input!" while True: name = input(temp) if name == 'q': break with open('guest.txt','a') as file_object: file_object.write(name) file_object.write("\n")
Please input your name! If you input 'q',then end the input!qian Please input your name! If you input 'q',then end the input!jian Please input your name! If you input 'q',then end the input!q
guest.txt文件的内容为:
qian jian
10-加法运算:
print("Please input two nmber") first = input("") second = input("") try : _first = int(first) _second = int(second) answer = + _first+_second print("The answer is " + str(answer)) except ValueError: print("At least one of the number is not number!")
Please input two nmber 1 3e At least one of the number is not number!