Skip to content

Commit

Permalink
修正了文档上的部分bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed May 3, 2019
1 parent 6411875 commit bd20a91
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 352 deletions.
91 changes: 45 additions & 46 deletions Day01-15/Day03/分支结构.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ password = input('请输入口令: ')
# import getpass
# password = getpass.getpass('请输入口令: ')
if username == 'admin' and password == '123456':
print('身份验证成功!')
print('身份验证成功!')
else:
print('身份验证失败!')
print('身份验证失败!')
```

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

x = float(input('x = '))
if x > 1:
y = 3 * x - 5
y = 3 * x - 5
elif x >= -1:
y = x + 2
y = x + 2
else:
y = 5 * x + 3
y = 5 * x + 3
print('f(%.2f) = %.2f' % (x, y))
```

Expand All @@ -70,12 +70,12 @@ Author: 骆昊

x = float(input('x = '))
if x > 1:
y = 3 * x - 5
y = 3 * x - 5
else:
if x >= -1:
y = x + 2
else:
y = 5 * x + 3
if x >= -1:
y = x + 2
else:
y = 5 * x + 3
print('f(%.2f) = %.2f' % (x, y))
```

Expand All @@ -96,11 +96,11 @@ Author: 骆昊
value = float(input('请输入长度: '))
unit = input('请输入单位: ')
if unit == 'in' or unit == '英寸':
print('%f英寸 = %f厘米' % (value, value * 2.54))
print('%f英寸 = %f厘米' % (value, value * 2.54))
elif unit == 'cm' or unit == '厘米':
print('%f厘米 = %f英寸' % (value, value / 2.54))
print('%f厘米 = %f英寸' % (value, value / 2.54))
else:
print('请输入有效的单位')
print('请输入有效的单位')
```

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

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

score = float(input('请输入成绩: '))
if score >= 90:
grade = 'A'
grade = 'A'
elif score >= 80:
grade = 'B'
grade = 'B'
elif score >= 70:
grade = 'C'
grade = 'C'
elif score >= 60:
grade = 'D'
grade = 'D'
else:
grade = 'E'
grade = 'E'
print('对应的等级是:', grade)
```
#### 练习4:输入三条边长如果能构成三角形就计算周长和面积
Expand All @@ -177,12 +177,12 @@ a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
if a + b > c and a + c > b and b + c > a:
print('周长: %f' % (a + b + c))
p = (a + b + c) / 2
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
print('面积: %f' % (area))
print('周长: %f' % (a + b + c))
p = (a + b + c) / 2
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
print('面积: %f' % (area))
else:
print('不能构成三角形')
print('不能构成三角形')
```
> **说明:**上面的代码中使用了`math`模块的`sqrt`函数来计算平方根。用边长计算三角形面积的公式叫做[海伦公式](https://zh.wikipedia.org/zh-hans/海伦公式)
Expand All @@ -200,32 +200,31 @@ salary = float(input('本月收入: '))
insurance = float(input('五险一金: '))
diff = salary - insurance - 3500
if diff <= 0:
rate = 0
deduction = 0
rate = 0
deduction = 0
elif diff < 1500:
rate = 0.03
deduction = 0
rate = 0.03
deduction = 0
elif diff < 4500:
rate = 0.1
deduction = 105
rate = 0.1
deduction = 105
elif diff < 9000:
rate = 0.2
deduction = 555
rate = 0.2
deduction = 555
elif diff < 35000:
rate = 0.25
deduction = 1005
rate = 0.25
deduction = 1005
elif diff < 55000:
rate = 0.3
deduction = 2755
rate = 0.3
deduction = 2755
elif diff < 80000:
rate = 0.35
deduction = 5505
rate = 0.35
deduction = 5505
else:
rate = 0.45
deduction = 13505
rate = 0.45
deduction = 13505
tax = abs(diff * rate - deduction)
print('个人所得税: ¥%.2f' % tax)
print('实际到手收入: ¥%.2f' % (diff + 3500 - tax))
```
>**说明:**上面的代码中使用了Python内置的`abs()`函数取绝对值来处理`-0`的问题。
58 changes: 27 additions & 31 deletions Day01-15/Day04/循环结构.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Author: 骆昊

sum = 0
for x in range(101):
sum += x
sum += x
print(sum)
```

Expand All @@ -40,7 +40,7 @@ Author: 骆昊

sum = 0
for x in range(2, 101, 2):
sum += x
sum += x
print(sum)
```

Expand All @@ -56,10 +56,9 @@ Author: 骆昊

sum = 0
for x in range(1, 101):
if x % 2 == 0:
sum += x
if x % 2 == 0:
sum += x
print(sum)

```

### while循环
Expand All @@ -81,18 +80,18 @@ import random
answer = random.randint(1, 100)
counter = 0
while True:
counter += 1
number = int(input('请输入: '))
if number < answer:
print('大一点')
elif number > answer:
print('小一点')
else:
print('恭喜你猜对了!')
break
counter += 1
number = int(input('请输入: '))
if number < answer:
print('大一点')
elif number > answer:
print('小一点')
else:
print('恭喜你猜对了!')
break
print('你总共猜了%d' % counter)
if counter > 7:
print('你的智商余额明显不足')
print('你的智商余额明显不足')
```

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

for i in range(1, 10):
for j in range(1, i + 1):
print('%d*%d=%d' % (i, j, i * j), end='\t')
print()
for j in range(1, i + 1):
print('%d*%d=%d' % (i, j, i * j), end='\t')
print()
```

### 练习
Expand All @@ -125,20 +124,19 @@ Version: 0.1
Author: 骆昊
Date: 2018-03-01
"""

from math import sqrt

num = int(input('请输入一个正整数: '))
end = int(sqrt(num))
is_prime = True
for x in range(2, end + 1):
if num % x == 0:
is_prime = False
break
if num % x == 0:
is_prime = False
break
if is_prime and num != 1:
print('%d是素数' % num)
print('%d是素数' % num)
else:
print('%d不是素数' % num)
print('%d不是素数' % num)
```

#### 练习2:输入两个正整数,计算最大公约数和最小公倍数。
Expand All @@ -155,13 +153,12 @@ Date: 2018-03-01
x = int(input('x = '))
y = int(input('y = '))
if x > y:
(x, y) = (y, x)
x, y = y, x
for factor in range(x, 0, -1):
if x % factor == 0 and y % factor == 0:
print('%d%d的最大公约数是%d' % (x, y, factor))
print('%d%d的最小公倍数是%d' % (x, y, x * y // factor))
break

if x % factor == 0 and y % factor == 0:
print('%d%d的最大公约数是%d' % (x, y, factor))
print('%d%d的最小公倍数是%d' % (x, y, x * y // factor))
break
```

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

Loading

0 comments on commit bd20a91

Please sign in to comment.