#9-2 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 restaurant's name is "+self.restaurant_name+'.') print("The restaurant's cuisine type is "+self.cuisine_type+'.') 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 restaurant_1=Restaurant("Costa","coffee") restaurant_2=Restaurant("BreadTalk","bread") restaurant_3=Restaurant("Pizzahot","pizza") restaurant_1.describe_restaurant() restaurant_1.open_restaurant() restaurant_2.describe_restaurant() restaurant_3.describe_restaurant() print('') #9-4 restaurant=Restaurant("restaurant","Chinese food",100) print(restaurant.number_served) restaurant.set_number_served(200) restaurant.increment_number_served(50) print('') #9-6 class IceCreamStand(Restaurant): def __init__(self,restaurant_name,cuisine_type,number_served=0): super().__init__(restaurant_name,cuisine_type,number_served=0) self.flavors=['Vanilla','Chocolate','Strawberry'] def show_flavors(self): print(self.flavors) icecreamStand=IceCreamStand('DQ','IceCream',2000) icecreamStand.show_flavors() print('') #10-1 with open('py.txt') as file_object: contents=file_object.read() print(contents) for obj in file_object: print(obj.rstrip()) lines=file_object.readlines() for line in lines: print(line.rstrip()) #10-4 filename='guest.txt' with open(filename,'w') as file_object: print("Please enter three guests' names:(Enter 'quit' to stop)") name=input() while name!='quit': print("Hello, "+name+'!') file_object.write(name) name=input() print('') #10-6 print("Enter two numbers and I will add them.(Enter 'quit' to stop)") while True: first_number=input("\nThe first number is: ") if first_number=='quit': break try: int(first_number) except ValueError: print("Sorry, you have to enter a number.") continue second_number=input("\nThe second number is: ") if second_number=='quit': break try: int(second_number) except ValueError: print("Sorry, you have to enter a number.") continue print(int(first_number)+int(second_number)) print('') #10-11 import json num=input("Please input your favorite number: ") filename='number.json' with open(filename,'w') as f_obj: json.dump(num,f_obj) with open(filename) as f_obj: print("I know your favorite number! It's "+json.load(f_obj)+'.')
输入输出结果如下:
The restaurant's name is Costa. The restaurant's cuisine type is coffee. The restaurant is open. The restaurant's name is BreadTalk. The restaurant's cuisine type is bread. The restaurant's name is Pizzahot. The restaurant's cuisine type is pizza. 100 ['Vanilla', 'Chocolate', 'Strawberry'] In Python you can do almost everything! In Python you can do exactly everything! Please enter three guests' names:(Enter 'quit' to stop) Sally Hello, Sally! Mary Hello, Mary! Nolan Hello, Nolan! quit Enter two numbers and I will add them.(Enter 'quit' to stop) The first number is: 1 The second number is: w Sorry, you have to enter a number. The first number is: e Sorry, you have to enter a number. The first number is: 1 The second number is: 2 3 The first number is: quit Please input your favorite number: 7 I know your favorite number! It's 7.