Skip to content

Commit 76d3640

Browse files
committed
增加了第二课逻辑判断的题目
1 parent 9e6292a commit 76d3640

3 files changed

Lines changed: 409 additions & 0 deletions

File tree

02-logic/answer-questions.ipynb

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 季节判断机器人\n",
8+
"* 在中国 1~3月为春季,4~6个月为夏季,7~9月为秋季,10~12月为冬季\n",
9+
"* 提示用户输入一个月份(数字),然后输出是什么季节\n",
10+
"* 如果用户输入 1~12 之外的数字,提示 \"机器故障\"\n",
11+
"\n",
12+
" 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读系统中的日期"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 7,
18+
"metadata": {
19+
"ExecuteTime": {
20+
"end_time": "2020-07-14T01:28:56.303344Z",
21+
"start_time": "2020-07-14T01:28:54.859365Z"
22+
}
23+
},
24+
"outputs": [
25+
{
26+
"name": "stdout",
27+
"output_type": "stream",
28+
"text": [
29+
"请入现在是几月:1000\n",
30+
"机器故障\n"
31+
]
32+
}
33+
],
34+
"source": [
35+
"month = int(input(\"请入现在是几月:\"))\n",
36+
"if month>=1 and month<=3:\n",
37+
" print(\"春季\")\n",
38+
"elif month>=4 and month<=6:\n",
39+
" print(\"夏季\")\n",
40+
"elif month>=7 and month<=9:\n",
41+
" print(\"秋季\")\n",
42+
"elif month>=10 and month<=12:\n",
43+
" print(\"冬季\")\n",
44+
"else:\n",
45+
" print(\"机器故障\")\n"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"# 价格预警机器人\n",
53+
"* 假设你要监控 A,B,C 三个商品\n",
54+
"* 如是果 A/B/C 分别降价 30/20/10 元上,就要报警\n",
55+
"* 提示用户输入商品名(A/B/C)\n",
56+
"* 提示用户输入降价值 (数字)\n",
57+
"* 如果需要报警则打印商品名 + 降价值\n"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 14,
63+
"metadata": {
64+
"ExecuteTime": {
65+
"end_time": "2020-07-14T01:35:14.721332Z",
66+
"start_time": "2020-07-14T01:33:44.855245Z"
67+
}
68+
},
69+
"outputs": [
70+
{
71+
"ename": "KeyboardInterrupt",
72+
"evalue": "",
73+
"output_type": "error",
74+
"traceback": [
75+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
76+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
77+
"\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 883\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 884\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 885\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
78+
"\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
79+
"\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 474\u001b[0m \"\"\"\n\u001b[0;32m--> 475\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 476\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
80+
"\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n",
81+
"\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n",
82+
"\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n",
83+
"\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n",
84+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: ",
85+
"\nDuring handling of the above exception, another exception occurred:\n",
86+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
87+
"\u001b[0;32m<ipython-input-14-c79612d80d46>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mproduct\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"请输入商品名:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mlower_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"请输入降价值:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mproduct\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"A\"\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlower_price\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m30\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[Alert] Product %s, lower_price %d\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mproduct\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlower_price\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mproduct\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"B\"\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlower_price\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
88+
"\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 857\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 858\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 859\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 860\u001b[0m )\n\u001b[1;32m 861\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
89+
"\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 887\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 888\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 889\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 890\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 891\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
90+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
91+
]
92+
}
93+
],
94+
"source": [
95+
"# 解法1\n",
96+
"product = input(\"请输入商品名:\")\n",
97+
"lower_price = int(input(\"请输入降价值:\"))\n",
98+
"if product == \"A\" and lower_price > 30:\n",
99+
" print(\"[Alert] Product %s, lower_price %d\" % (product,lower_price))\n",
100+
"elif product == \"B\" and lower_price > 20:\n",
101+
" print(\"[Alert] Product %s, lower_price %d\" % (product,lower_price))\n",
102+
"elif product == \"C\" and lower_price > 10:\n",
103+
" print(\"[Alert] Product %s, lower_price %d\" % (product,lower_price))\n",
104+
"else:\n",
105+
" print(\"No Alert!\")"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 15,
111+
"metadata": {
112+
"ExecuteTime": {
113+
"end_time": "2020-07-14T01:35:20.248871Z",
114+
"start_time": "2020-07-14T01:35:17.006007Z"
115+
}
116+
},
117+
"outputs": [
118+
{
119+
"name": "stdout",
120+
"output_type": "stream",
121+
"text": [
122+
"请输入商品名:A\n",
123+
"请输入降价值:31\n",
124+
"[Alert] Product A, lower_price 31\n"
125+
]
126+
}
127+
],
128+
"source": [
129+
"# 解法2\n",
130+
"product = input(\"请输入商品名:\")\n",
131+
"lower_price = int(input(\"请输入降价值:\"))\n",
132+
"if ((product == \"A\" and lower_price > 30) or \n",
133+
" (product == \"B\" and lower_price > 20) or\n",
134+
" (product == \"C\" and lower_price > 10)):\n",
135+
" print(\"[Alert] Product %s, lower_price %d\" % (product,lower_price))\n",
136+
"else:\n",
137+
" print(\"No Alert!\")"
138+
]
139+
},
140+
{
141+
"cell_type": "markdown",
142+
"metadata": {},
143+
"source": [
144+
"# 考试评语机器人\n",
145+
"* 假设你现在是考试院的老师,在看试卷,要打评语\n",
146+
"* 提示用户输出考试分数(数字)\n",
147+
"* 如果输入值小于 60 分,则打印 \"要加油呀!\"\n",
148+
"* 如果输入值大于等于 60 分,且小于 80分,则打印 \"不错呀,认真学习了!\"\n",
149+
"* 如果输入值大于等于 80 分,则打印 \"考得不错,不要骄傲!\"\n",
150+
"\n",
151+
" 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读取成绩表,直接出评语"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 19,
157+
"metadata": {
158+
"ExecuteTime": {
159+
"end_time": "2020-07-14T01:37:42.534729Z",
160+
"start_time": "2020-07-14T01:37:39.628546Z"
161+
}
162+
},
163+
"outputs": [
164+
{
165+
"name": "stdout",
166+
"output_type": "stream",
167+
"text": [
168+
"请输入分数:82\n",
169+
"考得不错,不要骄傲!\n"
170+
]
171+
}
172+
],
173+
"source": [
174+
"score = int(input(\"请输入分数:\"))\n",
175+
"if score < 60:\n",
176+
" print(\"要加油呀!\")\n",
177+
"elif score >=60 and score <80:\n",
178+
" print(\"不错呀,认真学习了!\")\n",
179+
"elif score >=80:\n",
180+
" print(\"考得不错,不要骄傲!\")"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"# 智能空调机器人\n",
188+
"* 假设你现在设计一台智能空调\n",
189+
"* 提示用户输入季节(只能是 winter/summer)\n",
190+
"* 提示用户输入当前温度 (数字)\n",
191+
"* 如果季节是 winter, 且温度低于10度,则打印 \"太冷了,开热空调了\"\n",
192+
"* 如果季节是 summer, 且温度高于27度,则打印 \"太热了,开冷空调了\"\n",
193+
"\n",
194+
" 备注:因为是学习的第二天,所以暂时让用户输入,以后可以接外部温度电子器直接读"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 22,
200+
"metadata": {
201+
"ExecuteTime": {
202+
"end_time": "2020-07-14T01:42:17.893109Z",
203+
"start_time": "2020-07-14T01:42:12.964759Z"
204+
}
205+
},
206+
"outputs": [
207+
{
208+
"name": "stdout",
209+
"output_type": "stream",
210+
"text": [
211+
"请输入季节(输入 winter/summer) :wooo\n",
212+
"请输入当前的温度(输入数字):100\n"
213+
]
214+
}
215+
],
216+
"source": [
217+
"season = input(\"请输入季节(输入 winter/summer) :\")\n",
218+
"temp = int(input(\"请输入当前的温度(输入数字):\"))\n",
219+
"if season == \"winter\" and temp < 10:\n",
220+
" print(\"太冷了,开热空调了\")\n",
221+
"elif season == \"summer\" and temp > 27:\n",
222+
" print(\"太热了,开冷空调了\")\n"
223+
]
224+
}
225+
],
226+
"metadata": {
227+
"kernelspec": {
228+
"display_name": "Python 3",
229+
"language": "python",
230+
"name": "python3"
231+
},
232+
"language_info": {
233+
"codemirror_mode": {
234+
"name": "ipython",
235+
"version": 3
236+
},
237+
"file_extension": ".py",
238+
"mimetype": "text/x-python",
239+
"name": "python",
240+
"nbconvert_exporter": "python",
241+
"pygments_lexer": "ipython3",
242+
"version": "3.7.6"
243+
},
244+
"toc": {
245+
"base_numbering": 1,
246+
"nav_menu": {},
247+
"number_sections": true,
248+
"sideBar": true,
249+
"skip_h1_title": false,
250+
"title_cell": "Table of Contents",
251+
"title_sidebar": "Contents",
252+
"toc_cell": false,
253+
"toc_position": {},
254+
"toc_section_display": true,
255+
"toc_window_display": false
256+
},
257+
"varInspector": {
258+
"cols": {
259+
"lenName": 16,
260+
"lenType": 16,
261+
"lenVar": 40
262+
},
263+
"kernels_config": {
264+
"python": {
265+
"delete_cmd_postfix": "",
266+
"delete_cmd_prefix": "del ",
267+
"library": "var_list.py",
268+
"varRefreshCmd": "print(var_dic_list())"
269+
},
270+
"r": {
271+
"delete_cmd_postfix": ") ",
272+
"delete_cmd_prefix": "rm(",
273+
"library": "var_list.r",
274+
"varRefreshCmd": "cat(var_dic_list()) "
275+
}
276+
},
277+
"types_to_exclude": [
278+
"module",
279+
"function",
280+
"builtin_function_or_method",
281+
"instance",
282+
"_Feature"
283+
],
284+
"window_display": false
285+
}
286+
},
287+
"nbformat": 4,
288+
"nbformat_minor": 4
289+
}

0 commit comments

Comments
 (0)