Skip to content

Commit bd20a91

Browse files
committed
修正了文档上的部分bug
1 parent 6411875 commit bd20a91

12 files changed

+315
-352
lines changed

Day01-15/Day03/分支结构.md

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ password = input('请输入口令: ')
2222
# import getpass
2323
# password = getpass.getpass('请输入口令: ')
2424
if username == 'admin' and password == '123456':
25-
print('身份验证成功!')
25+
print('身份验证成功!')
2626
else:
27-
print('身份验证失败!')
27+
print('身份验证失败!')
2828
```
2929

3030
唯一需要说明的是和C/C++、Java等语言不同,Python中没有用花括号来构造代码块而是使用了缩进的方式来设置代码的层次结构,如果`if`条件成立的情况下需要执行多条语句,只要保持多条语句具有相同的缩进就可以了,换句话说连续的代码如果又保持了相同的缩进那么它们属于同一个代码块,相当于是一个执行的整体。
@@ -47,11 +47,11 @@ Author: 骆昊
4747

4848
x = float(input('x = '))
4949
if x > 1:
50-
y = 3 * x - 5
50+
y = 3 * x - 5
5151
elif x >= -1:
52-
y = x + 2
52+
y = x + 2
5353
else:
54-
y = 5 * x + 3
54+
y = 5 * x + 3
5555
print('f(%.2f) = %.2f' % (x, y))
5656
```
5757

@@ -70,12 +70,12 @@ Author: 骆昊
7070

7171
x = float(input('x = '))
7272
if x > 1:
73-
y = 3 * x - 5
73+
y = 3 * x - 5
7474
else:
75-
if x >= -1:
76-
y = x + 2
77-
else:
78-
y = 5 * x + 3
75+
if x >= -1:
76+
y = x + 2
77+
else:
78+
y = 5 * x + 3
7979
print('f(%.2f) = %.2f' % (x, y))
8080
```
8181

@@ -96,11 +96,11 @@ Author: 骆昊
9696
value = float(input('请输入长度: '))
9797
unit = input('请输入单位: ')
9898
if unit == 'in' or unit == '英寸':
99-
print('%f英寸 = %f厘米' % (value, value * 2.54))
99+
print('%f英寸 = %f厘米' % (value, value * 2.54))
100100
elif unit == 'cm' or unit == '厘米':
101-
print('%f厘米 = %f英寸' % (value, value / 2.54))
101+
print('%f厘米 = %f英寸' % (value, value / 2.54))
102102
else:
103-
print('请输入有效的单位')
103+
print('请输入有效的单位')
104104
```
105105

106106
#### 练习2:掷骰子决定做什么
@@ -117,17 +117,17 @@ from random import randint
117117

118118
face = randint(1, 6)
119119
if face == 1:
120-
result = '唱首歌'
120+
result = '唱首歌'
121121
elif face == 2:
122-
result = '跳个舞'
122+
result = '跳个舞'
123123
elif face == 3:
124-
result = '学狗叫'
124+
result = '学狗叫'
125125
elif face == 4:
126-
result = '做俯卧撑'
126+
result = '做俯卧撑'
127127
elif face == 5:
128-
result = '念绕口令'
128+
result = '念绕口令'
129129
else:
130-
result = '讲冷笑话'
130+
result = '讲冷笑话'
131131
print(result)
132132
```
133133
> **说明:**上面的代码中使用了random模块的randint函数生成指定范围的随机数来模拟掷骰子。
@@ -149,15 +149,15 @@ Author: 骆昊
149149

150150
score = float(input('请输入成绩: '))
151151
if score >= 90:
152-
grade = 'A'
152+
grade = 'A'
153153
elif score >= 80:
154-
grade = 'B'
154+
grade = 'B'
155155
elif score >= 70:
156-
grade = 'C'
156+
grade = 'C'
157157
elif score >= 60:
158-
grade = 'D'
158+
grade = 'D'
159159
else:
160-
grade = 'E'
160+
grade = 'E'
161161
print('对应的等级是:', grade)
162162
```
163163
#### 练习4:输入三条边长如果能构成三角形就计算周长和面积
@@ -177,12 +177,12 @@ a = float(input('a = '))
177177
b = float(input('b = '))
178178
c = float(input('c = '))
179179
if a + b > c and a + c > b and b + c > a:
180-
print('周长: %f' % (a + b + c))
181-
p = (a + b + c) / 2
182-
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
183-
print('面积: %f' % (area))
180+
print('周长: %f' % (a + b + c))
181+
p = (a + b + c) / 2
182+
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
183+
print('面积: %f' % (area))
184184
else:
185-
print('不能构成三角形')
185+
print('不能构成三角形')
186186
```
187187
> **说明:**上面的代码中使用了`math`模块的`sqrt`函数来计算平方根。用边长计算三角形面积的公式叫做[海伦公式](https://zh.wikipedia.org/zh-hans/海伦公式)
188188
@@ -200,32 +200,31 @@ salary = float(input('本月收入: '))
200200
insurance = float(input('五险一金: '))
201201
diff = salary - insurance - 3500
202202
if diff <= 0:
203-
rate = 0
204-
deduction = 0
203+
rate = 0
204+
deduction = 0
205205
elif diff < 1500:
206-
rate = 0.03
207-
deduction = 0
206+
rate = 0.03
207+
deduction = 0
208208
elif diff < 4500:
209-
rate = 0.1
210-
deduction = 105
209+
rate = 0.1
210+
deduction = 105
211211
elif diff < 9000:
212-
rate = 0.2
213-
deduction = 555
212+
rate = 0.2
213+
deduction = 555
214214
elif diff < 35000:
215-
rate = 0.25
216-
deduction = 1005
215+
rate = 0.25
216+
deduction = 1005
217217
elif diff < 55000:
218-
rate = 0.3
219-
deduction = 2755
218+
rate = 0.3
219+
deduction = 2755
220220
elif diff < 80000:
221-
rate = 0.35
222-
deduction = 5505
221+
rate = 0.35
222+
deduction = 5505
223223
else:
224-
rate = 0.45
225-
deduction = 13505
224+
rate = 0.45
225+
deduction = 13505
226226
tax = abs(diff * rate - deduction)
227227
print('个人所得税: ¥%.2f' % tax)
228228
print('实际到手收入: ¥%.2f' % (diff + 3500 - tax))
229229
```
230230
>**说明:**上面的代码中使用了Python内置的`abs()`函数取绝对值来处理`-0`的问题。
231-

Day01-15/Day04/循环结构.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Author: 骆昊
1818

1919
sum = 0
2020
for x in range(101):
21-
sum += x
21+
sum += x
2222
print(sum)
2323
```
2424

@@ -40,7 +40,7 @@ Author: 骆昊
4040

4141
sum = 0
4242
for x in range(2, 101, 2):
43-
sum += x
43+
sum += x
4444
print(sum)
4545
```
4646

@@ -56,10 +56,9 @@ Author: 骆昊
5656

5757
sum = 0
5858
for x in range(1, 101):
59-
if x % 2 == 0:
60-
sum += x
59+
if x % 2 == 0:
60+
sum += x
6161
print(sum)
62-
6362
```
6463

6564
### while循环
@@ -81,18 +80,18 @@ import random
8180
answer = random.randint(1, 100)
8281
counter = 0
8382
while True:
84-
counter += 1
85-
number = int(input('请输入: '))
86-
if number < answer:
87-
print('大一点')
88-
elif number > answer:
89-
print('小一点')
90-
else:
91-
print('恭喜你猜对了!')
92-
break
83+
counter += 1
84+
number = int(input('请输入: '))
85+
if number < answer:
86+
print('大一点')
87+
elif number > answer:
88+
print('小一点')
89+
else:
90+
print('恭喜你猜对了!')
91+
break
9392
print('你总共猜了%d' % counter)
9493
if counter > 7:
95-
print('你的智商余额明显不足')
94+
print('你的智商余额明显不足')
9695
```
9796

9897
> **说明:**上面的代码中使用了`break`关键字来提前终止循环,需要注意的是`break`只能终止它所在的那个循环,这一点在使用嵌套的循环结构(下面会讲到)需要引起注意。除了`break`之外,还有另一个关键字是`continue`,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。
@@ -108,9 +107,9 @@ Author: 骆昊
108107
"""
109108

110109
for i in range(1, 10):
111-
for j in range(1, i + 1):
112-
print('%d*%d=%d' % (i, j, i * j), end='\t')
113-
print()
110+
for j in range(1, i + 1):
111+
print('%d*%d=%d' % (i, j, i * j), end='\t')
112+
print()
114113
```
115114

116115
### 练习
@@ -125,20 +124,19 @@ Version: 0.1
125124
Author: 骆昊
126125
Date: 2018-03-01
127126
"""
128-
129127
from math import sqrt
130128

131129
num = int(input('请输入一个正整数: '))
132130
end = int(sqrt(num))
133131
is_prime = True
134132
for x in range(2, end + 1):
135-
if num % x == 0:
136-
is_prime = False
137-
break
133+
if num % x == 0:
134+
is_prime = False
135+
break
138136
if is_prime and num != 1:
139-
print('%d是素数' % num)
137+
print('%d是素数' % num)
140138
else:
141-
print('%d不是素数' % num)
139+
print('%d不是素数' % num)
142140
```
143141

144142
#### 练习2:输入两个正整数,计算最大公约数和最小公倍数。
@@ -155,13 +153,12 @@ Date: 2018-03-01
155153
x = int(input('x = '))
156154
y = int(input('y = '))
157155
if x > y:
158-
(x, y) = (y, x)
156+
x, y = y, x
159157
for factor in range(x, 0, -1):
160-
if x % factor == 0 and y % factor == 0:
161-
print('%d%d的最大公约数是%d' % (x, y, factor))
162-
print('%d%d的最小公倍数是%d' % (x, y, x * y // factor))
163-
break
164-
158+
if x % factor == 0 and y % factor == 0:
159+
print('%d%d的最大公约数是%d' % (x, y, factor))
160+
print('%d%d的最小公倍数是%d' % (x, y, x * y // factor))
161+
break
165162
```
166163

167164
#### 练习3:打印三角形图案。
@@ -214,4 +211,3 @@ for i in range(row):
214211
print('*', end='')
215212
print()
216213
```
217-

0 commit comments

Comments
 (0)