文章目錄
- ??下載??
前言
也是一個(gè)??django?
??/??pywebio?
?的項(xiàng)目。
D:.
│ putMessage.py
│
└─server
│ Data
│ db.sqlite3
│ manage.py
│
└─server
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└─__pycache__
settings.cpython-36.pyc
urls.cpython-36.pyc
wsgi.cpython-36.pyc
__init__.cpython-36.pyc
實(shí)現(xiàn)了個(gè)什么效果?在線答題并統(tǒng)計(jì)答題結(jié)果。
正文
這個(gè)東西比我的那個(gè)登錄驗(yàn)證模板要簡(jiǎn)單一些。但是,這個(gè)項(xiàng)目也有一個(gè)有趣的東西。我們來看看吧。。
創(chuàng)建項(xiàng)目的命令:
django-admin startproject
1.??settings.py?
?
也是添加那一項(xiàng)??ALLOWED_HOSTS?
?? 的元素??*?
?,這里不再贅述。
2.??urls.py?
?
主要的服務(wù)器程序。
from django.shortcuts import HttpResponse
from django.urls import path
from json import dumps
avg = 0 # 記錄平均分
humen = 0 # 記錄人數(shù)
names = [] # 記錄答題人名字
avgtime = 0 # 記錄平均時(shí)間
def readAs(request): # 讀取和寫入
global humen,avg,names,avgtime
reads,data,temp = [],[],[]
with open('Data','r',encoding='utf-8') as f:
reads = f.read().splitlines()
# 添加請(qǐng)求來的數(shù)據(jù)
text = '%s的成績(jī)是%s,%s' % (request.GET['name'],request.GET['score'],request.GET['time'])
reads.append(text)
for i in range(0,len(reads)):
temp = reads[i].split('的成績(jī)是')
# 分割字符串、儲(chǔ)存字典
t = reads[i].split(',')
data.append({'name':temp[0],'score':float(temp[1].split(',')[0]),'time':int(t[1])})
del temp,reads
# 儲(chǔ)存數(shù)據(jù)
humen = len(data)
for i in range(0,humen):
avg += data[i]['score']
avg /= humen
for i in range(0,humen):
print(data[i]['name'])
names.append(data[i]['name'])
for i in range(0,len(data)):
avgtime += data[i]['time']
avgtime /= humen
# 重新寫入
with open('Data','a',encoding='utf-8') as f:
f.write(text + ' ')
return HttpResponse(dumps({'humen':str(humen),'avg':str(int(avg)),'time':str(int(avgtime))}))
def main(request):
return HttpResponse('<p><b>當(dāng)前答題情況:</b></p><font face="Courier New" color="green"><b><p>答題人數(shù):%d</p><p>答題平均分:%d</p><p>平均用時(shí):%s秒</p><p><a href="/answer" title="答題列表">答題列表</a></p></b></font>' % (humen,avg,avgtime))
def answerList(request): # 答題名單
string = ''
for i in range(0,len(names)):
string += '%s ' % names[i]
return HttpResponse(string)
urlpatterns = [
path('read/',readAs),
path('',main),
path('answer/',answerList)
]
3.??putMessage.py?
?
客戶端,發(fā)送請(qǐng)求用的。
from pywebio.input import *
from pywebio.output import *
from requests import get
from random import randint
from time import time
url = 'http://127.0.0.1:8000/' # 默認(rèn)IP
operate,answer = [],[]
right = 0 # 正答
n = 5 # 五道題
tempSco = 100 / n # 一道題的分?jǐn)?shù)
# 如果不能整除
if 100 % n:
n = 5
tempSco = 20
# 隨機(jī)生成運(yùn)算題
for i in range(0,n):
a,b,c = randint(1,9),randint(1,9),0
if a < b:
c = b
b = a
a = c
del c
operate.append('%d%s%d' % (a,['+','-','*'][randint(0,2)],b))
# 計(jì)算答案
for i in range(0,len(operate)):
answer.append(eval(operate[i]))
# 顯示
put_markdown('請(qǐng)你認(rèn)真答題!')
name = input('請(qǐng)輸入你的名字:')
if ('的成績(jī)是' in name) or (',' in name):
put_markdown('你的名字中含有非法字符,請(qǐng)修改。')
exit()
last = time() # 計(jì)時(shí)開始
for i in range(0,len(operate)):
ua = input('請(qǐng)回答:%s=?' % operate[i])
if ua == str(answer[i]):
right += 1
current = time() # 計(jì)時(shí)結(jié)束
right *= tempSco # 計(jì)算正確率
# 發(fā)送請(qǐng)求
x = int(current-last)
params = {
'name':name,
'score':right,
'time':x,
}
res = get(url + 'read/',params=params).json()
put_markdown('已經(jīng)有%s人提交,平均分是:%s,平均用時(shí):%s 你的分?jǐn)?shù):%f,你的用時(shí):%d' % (res['humen'],res['avg'],res['time'],int(right),x))
實(shí)現(xiàn)的效果
運(yùn)行服務(wù)器,打開客戶端。
5道題。
下載
點(diǎn)這下載。
本文摘自 :https://blog.51cto.com/u