Skip to content

Commit 7da25d0

Browse files
committed
添加第九章类的练习
1 parent 16bea3d commit 7da25d0

21 files changed

+574
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## 《Python编程从入门到实践》课后练习
2+
3+
### 第一部分-基础知识
24
- chapter02_变量和简单数据类型
35
- chapter03_列表简介
46
- chapter04_操作列表
@@ -9,3 +11,20 @@
911
- chapter09_类
1012
- chapter10_文件和异常
1113
- chapter11_测试代码
14+
15+
### 第二部分-项目
16+
17+
#### 项目1:外星人入侵
18+
- chapter12_武装飞船
19+
- chapter13_外星人
20+
- chapter14_记分
21+
22+
#### 项目2:数据可视化
23+
- chapter15_生成数据
24+
- chapter16_下载数据
25+
- chapter17_使用API
26+
27+
#### 项目3:Web应用程序
28+
- chapter18_Diango入门
29+
- chapter19_用户账户
30+
- chapter20_设置应用程序的样式并对其进行部署

chapter09/9-1.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# encoding: utf-8
2+
3+
class Restaurant():
4+
# 创建一个饭馆的类
5+
6+
def __init__(self, name, cuisine_type):
7+
self.name = name.title()
8+
self.cuisine_type = cuisine_type
9+
10+
def describe_restaurant(self):
11+
msg = self.name + " serves wonderful " + self.cuisine_type + "."
12+
print("\n" + msg)
13+
14+
def open_restaurant(self):
15+
msg = self.name + " is open. Come on in!"
16+
print("\n" + msg)
17+
18+
restaurant = Restaurant('the mean queen', 'pizza')
19+
print(restaurant.name)
20+
print(restaurant.cuisine_type)
21+
22+
restaurant.describe_restaurant()
23+
restaurant.open_restaurant()

chapter09/9-10.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from restaurant import Restaurant
2+
3+
channel_club = Restaurant('the channel club', 'steak and seafood')
4+
channel_club.describe_restaurant()
5+
channel_club.open_restaurant()

chapter09/9-11.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from user import Admin
2+
3+
eric = Admin('eric', 'matthes', 'e_matthes', '[email protected]', 'alaska')
4+
eric.describe_user()
5+
6+
eric_privileges = [
7+
'can reset passwords',
8+
'can moderate discussions',
9+
'can suspend accounts',
10+
]
11+
eric.privileges.privileges = eric_privileges
12+
13+
print("\nThe admin " + eric.username + " has these privileges: ")
14+
eric.privileges.show_privileges()

chapter09/9-12.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from admin import Admin
2+
3+
eric = Admin('eric', 'matthes', 'e_matthes', '[email protected]', 'alaska')
4+
eric.describe_user()
5+
6+
eric_privileges = [
7+
'can reset passwords',
8+
'can moderate discussions',
9+
'can suspend accounts',
10+
]
11+
eric.privileges.privileges = eric_privileges
12+
13+
print("\nThe admin " + eric.username + " has these privileges: ")
14+
eric.privileges.show_privileges()

chapter09/9-13.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import OrderedDict
2+
3+
glossary = OrderedDict()
4+
5+
glossary['string'] = 'A series of characters.'
6+
glossary['comment'] = 'A note in a program that the Python interpreter ignores.'
7+
glossary['list'] = 'A collection of items in a particular order.'
8+
glossary['loop'] = 'Work through a collection of items, one at a time.'
9+
glossary['dictionary'] = "A collection of key-value pairs."
10+
glossary['key'] = 'The first item in a key-value pair in a dictionary.'
11+
glossary['value'] = 'An item associated with a key in a dictionary.'
12+
glossary['conditional test'] = 'A comparison between two values.'
13+
glossary['float'] = 'A numerical value with a decimal component.'
14+
glossary['boolean expression'] = 'An expression that evaluates to True or False.'
15+
16+
for word, definition in glossary.items():
17+
print("\n" + word.title() + ": " + definition)

chapter09/9-14.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from random import randint
2+
3+
class Die():
4+
"""Represent a die, which can be rolled."""
5+
6+
def __init__(self, sides=6):
7+
"""Initialize the die."""
8+
self.sides = sides
9+
10+
def roll_die(self):
11+
"""Return a number between 1 and the number of sides."""
12+
return randint(1, self.sides)
13+
14+
d6 = Die()
15+
16+
results = []
17+
for roll_num in range(10):
18+
result = d6.roll_die()
19+
results.append(result)
20+
print("10 rolls of a 6-sided die:")
21+
print(results)
22+
23+
d10 = Die(sides=10)
24+
25+
results = []
26+
for roll_num in range(10):
27+
result = d10.roll_die()
28+
results.append(result)
29+
print("\n10 rolls of a 10-sided die:")
30+
print(results)
31+
32+
d20 = Die(sides=20)
33+
34+
results = []
35+
for roll_num in range(10):
36+
result = d20.roll_die()
37+
results.append(result)
38+
print("\n10 rolls of a 20-sided die:")
39+
print(results)

chapter09/9-2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# encoding: utf-8
2+
3+
class Restaurant():
4+
5+
def __init__(self, name, cuisine_type):
6+
#初始化
7+
self.name = name.title()
8+
self.cuisine_type = cuisine_type
9+
10+
def describe_restaurant(self):
11+
#展示饭馆信息
12+
msg = self.name + " serves wonderful " + self.cuisine_type + "."
13+
print("\n" + msg)
14+
15+
def open_restaurant(self):
16+
#饭馆信息
17+
msg = self.name + " is open. Come on in!"
18+
print("\n" + msg)
19+
20+
mean_queen = Restaurant('the mean queen', 'pizza')
21+
mean_queen.describe_restaurant()
22+
23+
ludvigs = Restaurant("ludvig's bistro", 'seafood')
24+
ludvigs.describe_restaurant()
25+
26+
mango_thai = Restaurant('mango thai', 'thai food')
27+
mango_thai.describe_restaurant()

chapter09/9-3.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# encoding: utf-8
2+
3+
class User():
4+
5+
def __init__(self, first_name, last_name, username, email, location):
6+
#初始化用户
7+
self.first_name = first_name.title()
8+
self.last_name = last_name.title()
9+
self.username = username
10+
self.email = email
11+
self.location = location.title()
12+
13+
def describe_user(self):
14+
print("\n" + self.first_name + " " + self.last_name)
15+
print(" Username: " + self.username)
16+
print(" Email: " + self.email)
17+
print(" Location: " + self.location)
18+
19+
def greet_user(self):
20+
print("\nWelcome back, " + self.username + "!")
21+
22+
eric = User('eric', 'matthes', 'e_matthes', '[email protected]', 'alaska')
23+
eric.describe_user()
24+
eric.greet_user()
25+
26+
willie = User('willie', 'burger', 'willieburger', '[email protected]', 'alaska')
27+
willie.describe_user()
28+
willie.greet_user()

chapter09/9-4.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# encoding: utf-8
2+
3+
class Restaurant():
4+
# 创建一个饭馆的类
5+
6+
def __init__(self, name, cuisine_type):
7+
self.name = name.title()
8+
self.cuisine_type = cuisine_type
9+
10+
def describe_restaurant(self):
11+
msg = self.name + " serves wonderful " + self.cuisine_type + "."
12+
print("\n" + msg)
13+
14+
def open_restaurant(self):
15+
msg = self.name + " is open. Come on in!"
16+
print("\n" + msg)
17+
# 设置就餐人数
18+
def set_number_served(self,number_served):
19+
self.number_served = number_served
20+
# 将就餐人数递增
21+
def increment_number_served(self, additional_served):
22+
self.number_served += additional_served
23+
24+
restaurant = Restaurant('the mean queen', 'pizza')
25+
restaurant.describe_restaurant()
26+
27+
print("\nNumber served: " + str(restaurant.set_number_served))
28+
restaurant.number_served = 430
29+
print("Number served: " + str(restaurant.number_served))
30+
31+
restaurant.set_number_served(1257)
32+
print("Number served: " + str(restaurant.number_served))
33+
34+
restaurant.increment_number_served(239)
35+
print("Number served: " + str(restaurant.number_served))

chapter09/9-5.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# encoding: utf-8
2+
3+
class User():
4+
5+
def __init__(self, first_name, last_name, username, email, location):
6+
#初始化用户
7+
self.first_name = first_name.title()
8+
self.last_name = last_name.title()
9+
self.username = username
10+
self.email = email
11+
self.location = location.title()
12+
self.login_attempts = 0
13+
14+
def describe_user(self):
15+
print("\n" + self.first_name + " " + self.last_name)
16+
print(" Username: " + self.username)
17+
print(" Email: " + self.email)
18+
print(" Location: " + self.location)
19+
20+
def greet_user(self):
21+
print("\nWelcome back, " + self.username + "!")
22+
23+
def increment_login_attempts(self):
24+
self.login_attempts += 1
25+
26+
def reset_login_attempts(self):
27+
self.login_attempts = 0
28+
29+
eric = User('eric', 'matthes', 'e_matthes', '[email protected]', 'alaska')
30+
eric.describe_user()
31+
eric.greet_user()
32+
33+
print("\nMaking 3 login attempts...")
34+
eric.increment_login_attempts()
35+
eric.increment_login_attempts()
36+
eric.increment_login_attempts()
37+
print(" Login attempts: " + str(eric.login_attempts))
38+
39+
print("Resetting login attempts...")
40+
eric.reset_login_attempts()
41+
print(" Login attempts: " + str(eric.login_attempts))

chapter09/9-6.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# encoding: utf-8
2+
3+
class Restaurant():
4+
# 创建一个饭馆的类
5+
6+
def __init__(self, name, cuisine_type):
7+
self.name = name.title()
8+
self.cuisine_type = cuisine_type
9+
10+
def describe_restaurant(self):
11+
msg = self.name + " serves wonderful " + self.cuisine_type + "."
12+
print("\n" + msg)
13+
14+
def open_restaurant(self):
15+
msg = self.name + " is open. Come on in!"
16+
print("\n" + msg)
17+
18+
# IceCreamStand继承Restaurant类
19+
class IceCreamStand(Restaurant):
20+
21+
def __init__(self, name, cuisine_type='ice_cream'):
22+
super().__init__(name, cuisine_type)
23+
self.flavors = [] # 存储一个由各种口味的冰淇淋组成的列表
24+
25+
def show_flavors(self):
26+
print("\nWe have the following flavors available:")
27+
for flavor in self.flavors:
28+
print("- " + flavor.title())
29+
30+
big_one = IceCreamStand('The Big One')
31+
big_one.flavors = ['vanilla', 'chocolate', 'black cherry']
32+
33+
big_one.describe_restaurant()
34+
big_one.show_flavors()

chapter09/9-7.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# encoding: utf-8
2+
3+
class User():
4+
5+
def __init__(self, first_name, last_name, username, email, location):
6+
#初始化用户
7+
self.first_name = first_name.title()
8+
self.last_name = last_name.title()
9+
self.username = username
10+
self.email = email
11+
self.location = location.title()
12+
13+
def describe_user(self):
14+
print("\n" + self.first_name + " " + self.last_name)
15+
print(" Username: " + self.username)
16+
print(" Email: " + self.email)
17+
print(" Location: " + self.location)
18+
19+
def greet_user(self):
20+
print("\nWelcome back, " + self.username + "!")
21+
22+
class Admin(User):
23+
def __init__(self, first_name, last_name, username, email, location):
24+
super().__init__(first_name, last_name, username, email, location)
25+
self.privileges = []
26+
27+
def show_privileges(self):
28+
for privilege in self.privileges:
29+
print("- " + privilege)
30+
31+
eric = Admin('eric', 'matthes', 'e_matthes', '[email protected]', 'alaska')
32+
eric.describe_user()
33+
34+
eric.privileges = [
35+
'can reset passwords',
36+
'can moderate discussions',
37+
'can suspend accounts',
38+
]
39+
40+
eric.show_privileges()

0 commit comments

Comments
 (0)