#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(self.restaurant_name,' ',self.cuisine_type)
def open_restaurant(self):
print("The restaurant is open")
a=Restaurant("A","type A")
print(a.restaurant_name)
print(a.cuisine_type)
a.describe_restaurant()
a.open_restaurant()
#9.3
class User():
def __init__(self,first_name,last_name):
self.first_name=first_name
self.last_name=last_name
def describe_user(self):
print(self.first_name,' ',self.last_name)
def greet_user(self):
print("Hello,",self.first_name)
a=User('K','L')
a.greet_user()
#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(self.restaurant_name,' ',self.cuisine_type)
def open_restaurant(self):
print("The restaurant is open")
def set_number_served(self,num):
self.number_served=num
def increase_number_served(self,num):
self.number_served+=num
#9.6
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors=['aa','bb','cc']
def showflavors(self):
print(self.flavors)
stand=IceCreamStand('a','b')
stand.showflavors()
#10.1
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.readlines()
for line in lines:
print(line.rstrip())
#10.2
with open('Learning_python.txt') as file_object:
lines=file_object.readlines()
for line in lines:
print(line.replace('python','C++').rstrip())
#10.3
name=input("Please enter your name: ")
with open('guest.txt','w') as file_object:
file_object.write(name)
#10.4
flag=1
with open('guest_book.txt','w') as file_object:
while flag:
name=input("Please enter your name: ")
c=input("continue? Y/N ")
if c=='N':
flag=0;
file_object.write(name)
file_object.write('\n')
#10.5
valid=1
a=input("Please input the first number: ")
try:
c=int(a)
except ValueError:
print("Invalid input!")
exit(1)
b=input("Please input the second number: ")
try:
d=int(b)
except ValueError:
print("Invalid input!")
exit(1)
print(c+d)