9-1~9-2
#coding:gbk
#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("restaurant's name is: " + self.restaurant_name)
print("Its cuisine_type is: " + self.cuisine_type)
def open_restaurant(self):
print("We are open.")
restaurant = Restaurant('26号餐厅', 'a')
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
print('-----------------------------------------')
'''
如果def describe_restaurant(self)和open_restaurant(self)忘记
在写上self形参,运行时会报告类似于:
TypeError: function() takes 0 positional arguments but 1 was given
的错误
'''
#9-2 三家餐馆
r1 = Restaurant('2号餐厅', 'a')
r2 = Restaurant('6号餐厅', 'b')
r3 = Restaurant('8号餐厅', 'c')
r1. describe_restaurant()
r2. describe_restaurant()
r3. describe_restaurant()
9-3
#coding:gbk
#9-3 用户
class User():
def __init__(self, first_name, last_name, age, gender):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.gender = gender
def describe_user(self):
print('Its name is ' + self.first_name + ' ' + self.last_name)
print('It is ' + str(self.age) + ' years old')
print('Its gender is: ' + self.gender)
def greet_user(self):
print('Hello, ' + self.first_name + ' ' + self.last_name)
p1 = User('Timmy', 'X', 24, 'male')
p2 = User('Tom', 'H', 26, 'male')
p1.describe_user()
p1.greet_user()
p2.describe_user()
p2.greet_user()
9-4
#coding:gbk
#9-4 就餐人数
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
number_served = 0
def describe_restaurant(self):
print("restaurant's name is: " + self.restaurant_name)
print("Its cuisine_type is: " + self.cuisine_type)
def open_restaurant(self):
print("We are open.")
def set_number_served(n):
number_served = n
def increment_number_served():
number_served++
9-5
#coding:gbk
#9-5 尝试登录次数
class User():
def __init__(self, first_name, last_name, age, gender, login_attempts):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.gender = gender
self.login_attempts = login_attempts
def describe_user(self):
print('Its name is ' + self.first_name + ' ' + self.last_name)
print('It is ' + str(self.age) + ' years old')
print('Its gender is: ' + self.gender)
def greet_user(self):
print('Hello, ' + self.first_name + ' ' + self.last_name)
def increment_login_attempts(self):
self.login_attempts = self.login_attempts + 1
def reset_login_attempts(self):
self.login_attempts = 0
p1 = User('Timmy', 'X', 24, 'male', 3)
p1.increment_login_attempts()
p1.increment_login_attempts()
p1.increment_login_attempts()
print(p1.login_attempts)
p1.reset_login_attempts()
print(p1.login_attempts)
9-6
#coding:gbk
#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("restaurant's name is: " + self.restaurant_name)
print("Its cuisine_type is: " + self.cuisine_type)
def open_restaurant(self):
print("We are open.")
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type, flavors):
super().__init__(restaurant_name, cuisine_type)
self.flavors = flavors
def print_icecream(self):
print(self.flavors)
fl = ['a', 'b', 'c']
i = IceCreamStand('2号店', 'k', fl)
i.print_icecream()
9-7
#coding:gbk
#9-7 管理员
class User():
def __init__(self, first_name, last_name, age, gender):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.gender = gender
def describe_user(self):
print('Its name is ' + self.first_name + ' ' + self.last_name)
print('It is ' + str(self.age) + ' years old')
print('Its gender is: ' + self.gender)
def greet_user(self):
print('Hello, ' + self.first_name + ' ' + self.last_name)
class Admin(User):
def __init__(self, first_name, last_name, age, gender, privileges):
super().__init__(first_name, last_name, age, gender)
self.privileges = privileges
def show_privileges(self):
print('管理员' + self.first_name + self.last_name + '的权限有:')
for pri in self.privileges:
print(pri)
pri = ["can add post", "can delete post", "can ban user"]
a = Admin('于', '舟', 20, 'female', pri)
a.show_privileges()
9-8
#coding:gbk
#9-8 权限
class Privileges():
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
print('管理员的权限有:')
for pri in self.privileges:
print(pri)
class User():
def __init__(self, first_name, last_name, age, gender):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.gender = gender
def describe_user(self):
print('Its name is ' + self.first_name + ' ' + self.last_name)
print('It is ' + str(self.age) + ' years old')
print('Its gender is: ' + self.gender)
def greet_user(self):
print('Hello, ' + self.first_name + ' ' + self.last_name)
class Admin(User):
def __init__(self, first_name, last_name, age, gender):
super().__init__(first_name, last_name, age, gender)
self.privileges = Privileges(["can add post", "can delete post", "can ban user"])
a = Admin('于', '舟', 20, 'female')
a.privileges.show_privileges()
10-1
#coding:gbk
print('------------1.读取整个文件------------')
with open('If by Life You Were Deceived.txt') as f:
content = f.read() #read()发挥一个长字符串
print(content)
print('\n------------1.遍历文件对象------------')
with open('If by Life You Were Deceived.txt') as f:
content = f.readlines() #readlines 返回一个列表,每一行为一个元素
for line in content:
print(line.rstrip())
print('\n-------1.存储为列表,在with外打印---------')
with open('If by Life You Were Deceived.txt') as f:
lines = f.readlines()
for line in lines:
print(line.rstrip())
10-2
print('\n--------------4.单词替换-------------')
for line in lines:
''' !注意:replace的作用是暂时的,这样写输出的还是
line.replace('life', 'a') 原来的单词
print(line)
'''
print(line.replace('life', 'someone').rstrip())
#coding:gbk
str = input("请输入你的名字: ")
with open('guest.txt', 'w') as f:
f.write(str)
10-4
#coding:gbk
flag = 1
with open('guest_book.txt', 'w') as f:
while(flag != '0'):
name = input("请输入你的名字,输入0退出:")
if name == '0':
break
print("你好, " + name)
f.write(name+'\n')
![](https://i-blog.csdnimg.cn/blog_migrate/df0434e10fe2d3b278b0f2dc5123e5bc.png)
10-6~10-7
#coding:gbk
print('这是一个简单加法计算器,输入quit退出\n')
while(1):
a = input('请输入数字a: ')
if a == 'quit':
break
b = input('请输入数字b: ')
if b == 'quit':
break
try:
c = int(a) + int(b)
except ValueError:
print('你输入的不是数字哦~\n')
else:
print('答案是:' + str(c) + '\n')
![](https://i-blog.csdnimg.cn/blog_migrate/7c0715e5fc3c1cdaf689c5393e2a43da.png)
10-8
#coding:gbk
files = ['cats.txt', 'dogs.txt']
for file in files:
try:
with open(file) as f:
content = f.read()
except FileNotFoundError:
print('找不到这个文件(╥╯^╰╥)\n')
else:
print(content)
10-9
#coding:gbk
files = ['cats.txt', 'dogs.txt']
for file in files:
try:
with open(file) as f:
content = f.read()
except FileNotFoundError:
#print('找不到这个文件(╥╯^╰╥)\n')
pass
else:
print(content)
10-10
#coding:gbk
textname = "Alice's Adventures in Wonderland.txt"
with open(textname, 'r') as f:
contents = f.read()
'''
except FileNotFoundError:
print('找不到文件(╥╯^╰╥)\n')
'''
words = contents.split()
counter = len(words)
num = contents.lower().count('Alice')
msg = '《爱丽丝梦游仙境》这本书大约有:' + str(counter) +'个单词'
print(msg + '\n其中’Alice‘这个单词出现了' + str(num) +'次。')