Skip to content

Commit f8544a6

Browse files
committed
chanllenge 1
0 parents  commit f8544a6

File tree

4 files changed

+379
-0
lines changed

4 files changed

+379
-0
lines changed

.gitignore

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/
139+
140+
.idea

001_1_minute_math.md

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# 1 minute math
2+
3+
## Requirements
4+
5+
1. Run the code in console using command line.
6+
2. It'll run for 1 minute.
7+
3. For every time it'll show 2 random numbers and random arithmetic operations such as add, subtract, multiply and divide. If the operation is divide then the divisor can not be 0.
8+
4. It'll judge if your answer is correct or not, then show next question.
9+
5. When time is up it'll show how many questions you answered and show the correct rate for total questions.
10+
11+
## What will we practice in this project?
12+
13+
- format print
14+
- while loop
15+
- if condition
16+
- list
17+
- exception handle
18+
- function
19+
- time package
20+
- random package
21+
22+
## A reference code
23+
24+
```python
25+
import time
26+
import random
27+
28+
29+
def get_divisor(n):
30+
'''
31+
Get a random divisor of n.
32+
:param n: The number
33+
:return: a divisor of n
34+
'''
35+
l = []
36+
for i in range(1, n + 1):
37+
if n % i == 0:
38+
l.append(i)
39+
return random.choice(l)
40+
41+
42+
if __name__ =='__main__':
43+
ops = ['+', '-', '*', '/']
44+
start_time = time.time()
45+
total = 0
46+
correct = 0
47+
questions = []
48+
# while seconds is less than 60
49+
while time.time() - start_time <= 60:
50+
a = random.randint(1, 99)
51+
op = random.choice(ops)
52+
if op == '/':
53+
# if op is '/' then b is a divisor of a
54+
b = get_divisor(a)
55+
else:
56+
b = random.randint(1, 99)
57+
# Get the correct answer
58+
a_op_b = '{}{}{}'.format(a, op, b)
59+
c = int(eval(a_op_b))
60+
61+
# Let user input answer
62+
try:
63+
ans = int(input('{} = '.format(a_op_b)))
64+
except:
65+
ans = ''
66+
67+
# To check if correct or not
68+
if time.time() - start_time <= 60:
69+
if c == ans:
70+
print('Correct! Time remain {} seconds.'.format(int(60 - (time.time() - start_time))))
71+
correct = correct + 1
72+
else:
73+
print('Wrong answer! Time remain {} seconds.'.format(int(60 - (time.time() - start_time))))
74+
total = total + 1
75+
questions.append('{}={}'.format(a_op_b, ans))
76+
77+
print('{} questions and your correct rate is {:.2f}%'.format(total, correct / total * 100))
78+
for q in questions:
79+
print(q)
80+
81+
```
82+
83+
## Run the demo
84+
85+
Please save the Python as 1.py and run it in console:
86+
87+
```
88+
python 1.py
89+
```
90+
91+
![Chanllenge 1](images/challenge1_1.png)
92+
93+
----
94+
95+
# 1分钟数学运算
96+
97+
## 项目需求
98+
99+
- 直接在控制台使用命令行运行
100+
- 程序运行之后倒计时1分钟之后结束
101+
- 随机出100以内的2个整数加减乘除运算题目(除法确保能够除尽,但除数不能为0)
102+
- 每出一道题目,由玩家给出答案,然后程序判断对错,接着出下一题,并且显示剩余时间
103+
- 1分钟时间结束,显示总题数和正确率(正确率精确到小数点后2位),并将之前的题目和答案显示出来
104+
105+
## 项目练习
106+
107+
- 格式化字符串输出
108+
- 循环
109+
- 条件判断
110+
- 列表
111+
- 异常处理
112+
- 自定义函数
113+
- 时间工具包
114+
- 随机工具包
115+
116+
## 项目参考代码
117+
118+
```python
119+
import time
120+
import random
121+
122+
123+
def get_divisor(n):
124+
'''
125+
随机获得一个数n的整数除数。
126+
:param n: 一个整数
127+
:return: 一个数n的整数除数
128+
'''
129+
l = []
130+
for i in range(1, n + 1):
131+
if n % i == 0:
132+
l.append(i)
133+
return random.choice(l)
134+
135+
136+
if __name__ =='__main__':
137+
ops = ['+', '-', '*', '/']
138+
start_time = time.time()
139+
total = 0
140+
correct = 0
141+
questions = []
142+
while time.time() - start_time <= 60:
143+
a = random.randint(1, 99)
144+
op = random.choice(ops)
145+
if op == '/':
146+
# 如果是除法,b为a的一个随机整数除数
147+
b = get_divisor(a)
148+
else:
149+
b = random.randint(1, 99)
150+
# 正确答案
151+
a_op_b = '{}{}{}'.format(a, op, b)
152+
c = int(eval(a_op_b))
153+
154+
# 让用户输入答案
155+
try:
156+
ans = int(input('{} = '.format(a_op_b)))
157+
except:
158+
ans = ''
159+
160+
# 检查是否正确
161+
if time.time() - start_time <= 60:
162+
if c == ans:
163+
print('正确!剩余时间{}秒。'.format(int(60 - (time.time() - start_time))))
164+
correct = correct + 1
165+
else:
166+
print('错误!剩余时间{}秒。'.format(int(60 - (time.time() - start_time))))
167+
total = total + 1
168+
questions.append('{}={}'.format(a_op_b, ans))
169+
170+
print('{}道题目,正确率 {:.2f}%。'.format(total, correct / total * 100))
171+
for q in questions:
172+
print(q)
173+
174+
```
175+
176+
## 测试运行
177+
178+
将代码保存为1.py,然后在控制台运行:
179+
180+
```
181+
python 1.py
182+
```
183+
184+
![挑战1](images/challenge1_1.png)

code/1/1.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import time
2+
import random
3+
4+
5+
def get_divisor(n):
6+
'''
7+
Get a random divisor of n.
8+
:param n: The number
9+
:return: a divisor of n
10+
'''
11+
l = []
12+
for i in range(1, n + 1):
13+
if n % i == 0:
14+
l.append(i)
15+
return random.choice(l)
16+
17+
18+
if __name__ == '__main__':
19+
ops = ['+', '-', '*', '/']
20+
start_time = time.time()
21+
total = 0
22+
correct = 0
23+
questions = []
24+
# while seconds is less than 60
25+
while time.time() - start_time <= 60:
26+
a = random.randint(1, 99)
27+
op = random.choice(ops)
28+
if op == '/':
29+
# if op is '/' then b is a divisor of a
30+
b = get_divisor(a)
31+
else:
32+
b = random.randint(1, 99)
33+
# Get the correct answer
34+
a_op_b = '{}{}{}'.format(a, op, b)
35+
c = int(eval(a_op_b))
36+
37+
# Let user input answer
38+
try:
39+
ans = int(input('{} = '.format(a_op_b)))
40+
except:
41+
ans = ''
42+
43+
# To check if correct or not
44+
if time.time() - start_time <= 60:
45+
if c == ans:
46+
print('Correct! Time remain {} seconds.'.format(int(60 - (time.time() - start_time))))
47+
correct = correct + 1
48+
else:
49+
print('Wrong answer! Time remain {} seconds.'.format(int(60 - (time.time() - start_time))))
50+
total = total + 1
51+
questions.append('{}={}'.format(a_op_b, ans))
52+
53+
print('{} questions and your correct rate is {:.2f}%'.format(total, correct / total * 100))
54+
for q in questions:
55+
print(q)

images/challenge1_1.png

699 KB
Loading

0 commit comments

Comments
 (0)