python编程实战5(类,异常)

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!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值