python作业05类&文件&异常

Description (class)

Select from exercise 9-1 to 9-15

Code (class)

9-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 9-3 用户 :创建一个名为User 的类,其中包含属性first_name 和last_name ,
# 还有用户简介通常会存储的其他几个属性。在类User 中定义一个名
# 为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。
# 创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。

class User():
def __init__(self, first_name, last_name):
self.first_name = first_name.title()
self.last_name = last_name.title()

def describe_user(self):
print(self.first_name+" "+self.last_name)

def greet_user(self):
print("Hello "+self.first_name+" "+self.last_name+"! ")


users = [User("kate", "snow"), User("jone", "ossaki"), User("lin", "li")]
for u in users:
u.describe_user()
u.greet_user()

result:

1
2
3
4
5
6
Kate Snow
Hello Kate Snow!
Jone Ossaki
Hello Jone Ossaki!
Lin Li
Hello Lin Li!

9-5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class User():
def __init__(self, first_name, last_name):
self.first_name = first_name.title()
self.last_name = last_name.title()
self.login_attempts = 0
--snip--
def increment_login_attempts(self):
self.login_attempts += 1

def reset_login_attempts(self):
self.login_attempts = 0

# 9-5 尝试登录次数 :在为完成练习9-3而编写的User 类中,添加一个名为login_attempts 的属性。编写一个名为increment_login_attempts() 的方法,
# 它将属性login_attempts 的值加1。再编写一个名为reset_login_attempts() 的方法,它将属性login_attempts 的值重置为0。
# 根据User 类创建一个实例,再调用方法increment_login_attempts() 多次。打印属性login_attempts 的值,确认它被正确地递增;然后,调用方
# 法reset_login_attempts() ,并再次打印属性login_attempts 的值,确认它被重置为0。

test = User("King", "Kong")
test.increment_login_attempts()
test.increment_login_attempts()
test.increment_login_attempts()
print(test.login_attempts)
test.reset_login_attempts()
print(test.login_attempts)

result:

1
2
3
0

9-7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 9-7 管理员 :管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承你为完成练习9-3或练习9-5而编写的User 类。
# 添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)
# 组成的列表。编写一个名为show_privileges() 的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。

class Admin(User):

def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = ["can add post", "can delete post", "can ban user"]

def show_privileges(self):
for p in self.privileges:
print(p)


admin_test = Admin("Briiza", "Chen")
admin_test.show_privileges()

result:

1
2
3
can add post
can delete post
can ban user

9-11

1
2
3
4
5
6
7
# 9-11 导入Admin 类 :以为完成练习9-8而做的工作为基础,将User 、Privileges 和Admin 类存储在一个模块中,
# 再创建一个文件,在其中创建一个Admin 实例并对其调用方法show_privileges() ,以确认一切都能正确地运行。

import nine

test_import = nine.Admin("Buluce","Xiang")
test_import.show_privileges()

nine is the module define User and Admin
result:

1
2
3
4

can add post
can delete post
can ban user

Description (file)

Select from exercise 10-1 to 10-16

Code (files)

10-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 10-1 Python学习笔记 :在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,
# 其中每一行都以“In Python you can”打头。将这个文件命名为learning_python.txt,
# 并将其存储到为完成本章练习而编写的程序所在的目录中。编写一个程序,它读取这个文件,并将你所写的内容打印三次:
# 第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在with 代码块外打印它们。

with open('learning_python.txt') as file_obj:
content = file_obj.read()
print(content)

with open('learning_python.txt') as file_obj:
for item in file_obj:
print(item)

with open('learning_python.txt') as file_obj:
lines = file_obj.readlines()

for line in lines:
print(line)

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
In Python you can code.
In Python you can create a game.
In Python you can use loop.
In Python you can code.

In Python you can create a game.

In Python you can use loop.
In Python you can code.

In Python you can create a game.

In Python you can use loop.

10-4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 10-4 访客名单 :编写一个while 循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,
# 并将一条访问记录添加到文件guest_book.txt中。确保这个文件中的每条记录都独占一行。

with open('guest_book.txt', 'a') as file_obj:
while(1):
name = input("Input your name(q to quit): ")
if ( name == "q"):
break
print("Hello "+name+" !")
file_obj.write(name+"\n")

with open('guest_book.txt','r') as read_obj:
context = read_obj.read()
print(context)

result:

1
2
3
4
5
6
7
8
9
10
Input your name(q to quit): wang
Hello wang !
Input your name(q to quit): hey
Hello hey !
Input your name(q to quit): june
Hello june !
Input your name(q to quit): q
wang
hey
june

10-6 10-7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 10-6 加法运算 :提示用户提供数值输入时,常出现的一个问题是,用户提供的是文本而不是数字。在这种情况下,
# 当你尝试将输入转换为整数时,将引发TypeError 异常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。
# 在用户输入的任何一个值不是数字时都捕获TypeError 异常,并打印一条友好的错误消息。对你编写的程序进行测试:先输入两个数字,
# 再输入一些文本而不是数字。
# 10-7 加法计算器 :将你为完成练习10-6而编写的代码放在一个while 循环中,让用户犯错(输入的是文本而不是数字)后能够继续输入数字。

while (1):
a = input("Input 2 num, q to quit:")
b = input()
if (a=='q' or b=='q'):
break
try:
aa = int(a)
bb = int(b)
except:
print("Hey, error input, try again...")
else:
print(aa+bb)

result:

1
2
3
4
5
6
7
8
Input 2 num, q to quit:a
b
Hey, error input, try again...
Input 2 num, q to quit:2
3
5
Input 2 num, q to quit:q
f