崩潰-ing
西安已經下了兩周雨了,大家以為到頭了么?不僅僅才是開始,未來的一個月還要接著下,簡直崩潰啊!
昨天被逼著去加班,所以早期發了一篇娛樂帖,還好沒有掉粉…要謝謝各位的包容啊,哈哈。
學英語廣告
最近也許是剛開學的原因,不管是公眾號,還是刷抖音,導出都能看到關于學英語、背單詞的廣告。
不知道現在學生們背單詞買的什么輔導材料。反正我們上學那會,**《星火閱讀》**特別的火。記得當時隨書還送一個紅色的塑料膜。書中英語單詞是紅色的其他文字是黑色的。背單詞的時候先把塑料膜蓋在書上,然后就只能看到翻譯和音標,從而起到自測英語的作用。一頁看完了取下塑料膜,再核對哪些單詞記錯了。就這么一個無腦的功能,當時的我們都覺得好犀利,誰沒一本這樣的背單詞書,都不好意思出去裝13啊!
今天,我們就使用Python來做一個英語單詞自測工具!
需求分析
既然上面說了那么多的懷舊梗,那今天就仿照著從前的方式,做一個稍微高端一些的單詞自測工具。
先來看看實現效果吧…程序輸入你想測試的單詞數量,然后系統自動生成html測試題,之后你就可以通過速記與查看來檢測那些單詞你沒記住嘍…
找單詞
背單詞我們得先有單詞吧?從百度找了一篇2019cet4英語單詞表!
左圖下載的word文檔的內容包含各種廣告,為了方便,我直接把它全部拷貝存在文本文檔中,類似右圖。
觀察保存的文本內容,我們可以通過 ***斜杠’/’***將單詞、音標、翻譯進行拆分。
生成測試題
我們準備好了試題,怎么生成測試題呢?之前學習excel讀寫的時候,寫過一篇英語單詞自測的文章:
先生成單詞音標,然后用戶輸入翻譯,最后再D列追加正確的翻譯…
最近沒怎么學習web端的知識,所以今天我們來寫一套自動生成html測試題的練習吧!
準備基礎html文檔root.html:
<
html
lang
=
"
en
"
>
<
head
>
<
meta
charset
=
"
UTF-8
"
>
<
title
>
清風Python英語自測工具
title
>
<
link
rel
=
"
icon
"
href
=
"
basic/favicon.ico
"
>
<
link
href
=
"
basic/bootstrap.min.css
"
rel
=
"
stylesheet
"
>
<
link
href
=
"
basic/index.css
"
rel
=
"
stylesheet
"
>
<
script
src
=
"
basic/jquery.min.js
"
>
script
>
head
>
<
body
>
<
div
class
=
"
container
"
>
<
h3
class
=
"
title
"
>
清風Python英語單詞自測工具
h3
>
<
table
class
=
"
table table-striped table-hover
"
>
<
thead
>
<
td
>
序號
td
>
<
td
>
翻譯
td
>
<
td
>
音標
td
>
<
td
>
單詞
td
>
<
td
>
翻牌
td
>
thead
>
<
tbody
>
{content}
tbody
>
table
>
div
>
body
>
<
script
>
$
(
"button"
)
.
click
(
function
(
)
{
var
word
=
$
(
"."
+
$
(
this
)
.
attr
(
'line'
)
)
;
if
(
word
.
is
(
':visible'
)
)
{
word
.
slideUp
(
)
;
}
else
{
word
.
slideDown
(
)
;
}
}
)
;
script
>
html
>
其中content的內容,為我們等下自動生成試題…
其中引入的bootstrap、jQuery,都放在代碼同級的basic.html文件夾中…
Python代碼編寫
Python的代碼實現起來也比較簡單,讀取用戶測試數量,然后random獲取隨機測試內容,拆分數據后進行html內容組裝,最終生成自測html練習題:
# -*- coding: utf-8 -*-
# @Author : 王翔
# @WeChat : King_Uranus
# @公眾號 : 清風Python
# @Date : 2019/9/16 01:14
# @Software : PyCharm
# @version :Python 3.7.3
# @File : EnglishWordsTest.py
import
os
import
random
import
re
class
EnglishWordsTest
:
def
__init__
(
self
)
:
self
.
root_path
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)
)
with
open
(
os
.
path
.
join
(
self
.
root_path
,
'basic'
,
'cet4.txt'
)
,
encoding
=
'utf-8'
)
as
f
:
_all_words
=
f
.
readlines
(
)
self
.
html
=
""
self
.
clean_data
(
random
.
sample
(
_all_words
,
text_num
)
)
def
clean_data
(
self
,
data
)
:
exam_data
=
list
(
map
(
lambda
x
:
re
.
sub
(
"\s"
,
''
,
x
)
.
split
(
'/'
)
,
data
)
)
for
num
,
line
in
enumerate
(
exam_data
,
start
=
1
)
:
self
.
html
+=
"""
{0}
{3}
{2}
{1}
"""
.
format
(
num
,
*
line
)
with
open
(
os
.
path
.
join
(
self
.
root_path
,
'basic'
,
'root.html'
)
,
encoding
=
'utf-8'
)
as
f
:
data
=
f
.
read
(
)
with
open
(
os
.
path
.
join
(
self
.
root_path
,
'exam.html'
)
,
'w+'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
data
.
replace
(
'{content}'
,
self
.
html
)
)
if
__name__
==
'__main__'
:
print
(
"請輸入所需測試的單詞數量(范圍:1-100):"
)
while
True
:
try
:
text_num
=
int
(
input
(
)
)
if
1
<
text_num
<
100
:
break
except
ValueError
:
pass
print
(
"請仔細閱讀輸入范圍!"
)
EnglishWordsTest
(
)
關于文件
cet4的單詞、涉及到的css、js基礎模板,就不在文章中贅述了…
如果大家喜歡這個Python的英語測試題聯系,公眾號后臺回復
學英語
即可獲取整套代碼及文件。
The End
OK,今天的內容就到這里,如果覺得內容對你有所幫助,歡迎點擊文章右下角的“
在看
”。
期待你關注我的公眾號
清風Python
,如果覺得不錯,希望能動動手指轉發給你身邊的朋友們。
希望每周一至五清晨的7點10分,都能讓清風Python的知識文章叫醒大家!謝謝……
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
