Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2785244

Browse files
feat(day03): 新增 Day03 学习笔记
1 parent a8f28b4 commit 2785244

File tree

7 files changed

+369
-0
lines changed

7 files changed

+369
-0
lines changed

‎day03/code/practice1.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
英制单位英寸和公制单位厘米互换
3+
1英寸(in) = 2.54厘米(cm)
4+
5+
输入:长度 value 与 单位 unit
6+
输出:另外一类的长度与单位
7+
8+
Version: 1.0.0
9+
Author: Jalan
10+
"""
11+
12+
value = float(input("请输入长度:"))
13+
unit = input("请输入单位:")
14+
15+
# 如果输入的是厘米
16+
if unit == "cm" or unit == "厘米":
17+
output_value = value / 2.54
18+
print("%f 厘米 = %f 英寸" % (value, output_value))
19+
# 如果输入的是英寸
20+
elif unit == "in" or unit == "英寸":
21+
output_value = value * 2.54
22+
print("%f 英寸 = %f 厘米" % (value, output_value))
23+
# 输入的单位无效
24+
else:
25+
print("请输入有效单位")

‎day03/code/practice2-1.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
掷骰子决定做什么事情
3+
摇出 1~6 的数字,判断要做什么事
4+
5+
Version: 1.0.0
6+
Author: Jalan
7+
"""
8+
9+
from random import randint
10+
11+
d = {1: "吃饭", 2: "睡觉", 3: "喵喵叫", 4: "汪汪叫", 5: "打豆豆", 6: "写代码"}
12+
# 产生 1-6 的随机数
13+
num = randint(1, 6)
14+
15+
print(d[num])

‎day03/code/practice2.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
掷骰子决定做什么事情
3+
摇出 1~6 的数字,判断要做什么事
4+
5+
Version: 1.0.0
6+
Author: Jalan
7+
"""
8+
9+
from random import randint
10+
11+
# 产生 1-6 的随机数
12+
num = randint(1, 6)
13+
14+
if num == 1:
15+
res = "吃饭"
16+
elif num == 2:
17+
res = "睡觉"
18+
elif num == 3:
19+
res = "喵喵叫"
20+
elif num == 4:
21+
res = "汪汪叫"
22+
elif num == 5:
23+
res = "打豆豆"
24+
elif num == 6:
25+
res = "写代码"
26+
27+
print(res)

‎day03/code/practice3.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
百分制成绩转等级制成绩
3+
90分以上 --> A
4+
80分~89分 --> B
5+
70分~79分 --> C
6+
60分~69分 --> D
7+
60分以下 --> E
8+
9+
Version: 1.0.0
10+
Author: Jalan
11+
"""
12+
13+
score = float(input("请输入成绩:"))
14+
15+
if score >= 90:
16+
grade = "A"
17+
elif score >= 80:
18+
grade = "B"
19+
elif score >= 70:
20+
grade = "C"
21+
elif score >= 60:
22+
grade = "D"
23+
else:
24+
grade = "E"
25+
26+
print("成绩等级是:%s" % grade)

‎day03/code/practice4.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
判断输入的边长能否构成三角形
3+
如果能则计算出三角形的周长和面积
4+
海伦公式:area = sqrt(p(p-a)(p-b)(p-c))
5+
6+
Version: 1.0.0
7+
Author: Jalan
8+
"""
9+
import math
10+
11+
a = float(input("a = "))
12+
b = float(input("b = "))
13+
c = float(input("c = "))
14+
15+
if a + b > c and a + c > b and b + c > a:
16+
p = a + b + c
17+
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
18+
print("周长 = %.2f,面积 = %.2f" % (p, area))
19+
else:
20+
print("无法构成三角形")

‎day03/code/practice5.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
输入月收入和五险一金计算个人所得税
3+
4+
Version 1.0.0
5+
Author: Jalan
6+
"""
7+
8+
salary = float(input("本月收入:"))
9+
insurance = float(input("五险一金:"))
10+
diff = salary - insurance - 5000
11+
12+
if diff <= 0:
13+
rate = 0
14+
deduction = 0
15+
elif diff < 3000:
16+
rate = 0.03
17+
deduction = 0
18+
elif diff < 12000:
19+
rate = 0.1
20+
deduction = 210
21+
elif diff < 25000:
22+
rate = 0.2
23+
deduction = 1410
24+
elif diff < 35000:
25+
rate = 0.25
26+
deduction = 2660
27+
elif diff < 55000:
28+
rate = 0.3
29+
deduction = 4410
30+
elif diff < 80000:
31+
rate = 0.35
32+
deduction = 7160
33+
else:
34+
rate = 0.45
35+
deduction = 15160
36+
37+
tax = abs(diff * rate - deduction)
38+
print('个人所得税: \%.2f元' % tax)
39+
print('实际到手收入: \%.2f元' % (diff + 5000 - tax))

‎day03/index.md‎

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# Day03 - 分支结构
2+
3+
- Python 中使用 `elif` 而非像别的语言那样是 `elseif`
4+
- **Flat is better than nested**:尽量减少嵌套次数
5+
6+
## 练习
7+
8+
### 练习1:英制单位与公制单位互换
9+
10+
```python
11+
"""
12+
英制单位英寸和公制单位厘米互换
13+
1英寸(in) = 2.54厘米(cm)
14+
15+
输入:长度 value 与 单位 unit
16+
输出:另外一类的长度与单位
17+
18+
Version: 1.0.0
19+
Author: Jalan
20+
"""
21+
22+
value = float(input("请输入长度:"))
23+
unit = input("请输入单位:")
24+
25+
# 如果输入的是厘米
26+
if unit == "cm" or unit == "厘米":
27+
output_value = value / 2.54
28+
print("%f 厘米 = %f 英寸" % (value, output_value))
29+
# 如果输入的是英寸
30+
elif unit == "in" or unit == "英寸":
31+
output_value = value * 2.54
32+
print("%f 英寸 = %f 厘米" % (value, output_value))
33+
# 输入的单位无效
34+
else:
35+
print("请输入有效单位")
36+
```
37+
38+
注意点:判断输入的单位有效性
39+
40+
### 练习2:掷骰子决定做什么
41+
42+
```
43+
"""
44+
掷骰子决定做什么事情
45+
摇出 1~6 的数字,判断要做什么事
46+
47+
Version: 1.0.0
48+
Author: Jalan
49+
"""
50+
51+
from random import randint
52+
53+
# 产生 1-6 的随机数
54+
num = randint(1, 6)
55+
56+
if num == 1:
57+
res = "吃饭"
58+
elif num == 2:
59+
res = "睡觉"
60+
elif num == 3:
61+
res = "喵喵叫"
62+
elif num == 4:
63+
res = "汪汪叫"
64+
elif num == 5:
65+
res = "打豆豆"
66+
elif num == 6:
67+
res = "写代码"
68+
69+
print(res)
70+
```
71+
72+
知识点:使用 `random` 模块的 `randint` 函数生成特定范围的随机数。
73+
74+
`random` 常见函数如下:
75+
76+
```python
77+
#!/usr/bin/python
78+
# -*- coding: UTF-8 -*-
79+
80+
import random
81+
82+
print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数
83+
print( random.random() ) # 产生 0 到 1 之间的随机浮点数
84+
print( random.uniform(1.1,5.4) ) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
85+
print( random.choice('tomorrow') ) # 从序列中随机选取一个元素
86+
print( random.randrange(1,100,2) ) # 生成从1到100的间隔为2的随机整数
87+
88+
a=[1,3,5,6,7] # 将序列a中的元素顺序打乱
89+
random.shuffle(a)
90+
print(a)
91+
```
92+
93+
这道题如果不属于分支结构分类的话,我可能会用字典来写:
94+
95+
```python
96+
"""
97+
掷骰子决定做什么事情
98+
摇出 1~6 的数字,判断要做什么事
99+
100+
Version: 1.0.0
101+
Author: Jalan
102+
"""
103+
104+
from random import randint
105+
106+
d = {1: "吃饭", 2: "睡觉", 3: "喵喵叫", 4: "汪汪叫", 5: "打豆豆", 6: "写代码"}
107+
# 产生 1-6 的随机数
108+
num = randint(1, 6)
109+
110+
print(d[num])
111+
```
112+
113+
### 练习3:百分制成绩转等级制
114+
115+
```python
116+
"""
117+
百分制成绩转等级制成绩
118+
90分以上 --> A
119+
80分~89分 --> B
120+
70分~79分 --> C
121+
60分~69分 --> D
122+
60分以下 --> E
123+
124+
Version: 1.0.0
125+
Author: Jalan
126+
"""
127+
128+
score = float(input("请输入成绩:"))
129+
130+
if score >= 90:
131+
grade = "A"
132+
elif score >= 80:
133+
grade = "B"
134+
elif score >= 70:
135+
grade = "C"
136+
elif score >= 60:
137+
grade = "D"
138+
else:
139+
grade = "E"
140+
141+
print("成绩等级是:%s" % grade)
142+
```
143+
144+
### 练习4:输入三条边长如果能构成三角形就计算周长和面积
145+
146+
```python
147+
"""
148+
判断输入的边长能否构成三角形
149+
如果能则计算出三角形的周长和面积
150+
海伦公式:area = sqrt(p(p-a)(p-b)(p-c))
151+
152+
Version: 1.0.0
153+
Author: Jalan
154+
"""
155+
import math
156+
157+
a = float(input("a = "))
158+
b = float(input("b = "))
159+
c = float(input("c = "))
160+
161+
if a + b > c and a + c > b and b + c > a:
162+
p = a + b + c
163+
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
164+
print("周长 = %.2f,面积 = %.2f" % (p, area))
165+
else:
166+
print("无法构成三角形")
167+
```
168+
169+
完全忘记三角形面积怎么算了......😓然后复习了海伦公式......
170+
171+
### 练习5:个人所得税计算器
172+
173+
```python
174+
"""
175+
输入月收入和五险一金计算个人所得税
176+
177+
Version 1.0.0
178+
Author: Jalan
179+
"""
180+
181+
salary = float(input("本月收入:"))
182+
insurance = float(input("五险一金:"))
183+
diff = salary - insurance - 5000
184+
185+
if diff <= 0:
186+
rate = 0
187+
deduction = 0
188+
elif diff < 3000:
189+
rate = 0.03
190+
deduction = 0
191+
elif diff < 12000:
192+
rate = 0.1
193+
deduction = 210
194+
elif diff < 25000:
195+
rate = 0.2
196+
deduction = 1410
197+
elif diff < 35000:
198+
rate = 0.25
199+
deduction = 2660
200+
elif diff < 55000:
201+
rate = 0.3
202+
deduction = 4410
203+
elif diff < 80000:
204+
rate = 0.35
205+
deduction = 7160
206+
else:
207+
rate = 0.45
208+
deduction = 15160
209+
210+
tax = abs(diff * rate - deduction)
211+
print('个人所得税: \%.2f' % tax)
212+
print('实际到手收入: \%.2f' % (diff + 5000 - tax))
213+
```
214+
215+
个税表可以参考:[2018年新版个税计算器(5000起征点)](http://www.chineseacc.com/tool/gsjsq/2018-07-31/2923.html)
216+
217+
知识点:`abs()` 用于处理 `-0` 的问题

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /