计算分数算法

1.最简单的计算分数

correct_answer=["A","B","C"]
my_answer=["A","C","D"]
mark=0
i=0
while i<3:
    if correct_answer[i]==my_answer[i]:
        mark+=1
    i=i+1
print(mark)	
#运行结果:
1

2.分模块计算

correct_answer=["A","B","C"]
my_answer=["A","C","D"]
mark=0
i=0
base_mark=0
promote_mark=0
major_mark=0
while i<3:
    if i<=1:
        if correct_answer[i]==my_answer[i]:
            base_mark+=1
            mark=mark+1
    else:
        if correct_answer[i]==my_answer[i]:
            mark=mark+1
    i=i+1
print("总分数为:",mark)
print("基础分数为:",base_mark)

3.分三个模块进行计算

correct_answer=["A","B","C","D","A","B","C","D","A","B"]
my_answer=["A","C","C","A","C","D","C","C","D","A"]
mark=0
i=0
base_mark=0
promote_mark=0
major_mark=0
promote_mark=0
major_mark=0
while i<=9:
    if i<=3:
        if correct_answer[i]==my_answer[i]:
            base_mark+=1
            mark=mark+1
    elif i<=6:
        if correct_answer[i]==my_answer[i]:
            mark=mark+1
            promote_mark+=1
    else:
        if correct_answer[i]==my_answer[i]:
            mark=mark+1
            major_mark+=1	
    i=i+1
print("总分数为:",mark)
print("基础分数为:",base_mark)
print("提升分数为:",promote_mark)
print("专业分数为:",major_mark)

在这里插入图片描述
4.html实现表单选项

		<div class="layui-form-item" pane="">
			<label class="layui-form-label">单选框label>
			<div class="layui-input-block">
				<input type="radio" name="sex" value="" title="">A选项
				<input type="radio" name="sex" value="" title="">B选项
				<input type="radio" name="sex" value="" title="">C选项
				<input type="radio" name="sex" value="" title="">D选项
				<input type="submit" value="提交">
			div>
		div>

在这里插入图片描述

4.下午的工作

#views.py
from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse
from .models import Question,Exam,Subject
from django.db import models
# Create your views here.
class ExamView(View):
    def get(self,request):
        subjetcs=Subject.objects.get(id=1)
        print(subjetcs)
        question=Question.objects.get(id=1)
        print(id)
        context={
            "question_statements":question.statments,
            "op_A": question.op_A,
            "op_B": question.op_B,
            "op_C": question.op_C,
            "op_D": question.op_D,
        }

        return render(request, "exam.html",context=context)
    def post(self,request):
        question = request.POST.get('question')
        question2=request.POST.get('question2')
        options_list=re(request)
        print(question)
        print(question2)
        my_answer=[]
        my_answer.append(question)
        my_answer.append(question2)
        print(my_answer)
        print(options_list)
        return HttpResponse("success")
def re(request):
    option_list = []
    options=Question.objects.all()
    for option in options:
        option_list.append(option.correct_op)
    return option_list
#urls.py
from django.urls import path
from .views import ExamView
urlpatterns=[
    path("exam01/",ExamView.as_view())
]
#models.py
from django.db import models
from users.models import User

# Create your models here.

#科目
class Subject(models.Model):
	name 			= models.CharField(max_length=255,verbose_name="科目名称")
	description 	= models.TextField(verbose_name="科目描述")
	cover_image 	= models.ImageField(upload_to = 'cover/',blank=True)
	class Meta:
		verbose_name="科目"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.name

class Question(models.Model):
	choice = (('A','A'),('B','B'),('C','C'),('D','D'))
	subject 	= models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	statments 	= models.TextField(verbose_name="问题描述")
	op_A 	= models.CharField(max_length=1000)
	op_B 	= models.CharField(max_length=1000)
	op_C 	= models.CharField(max_length=1000)
	op_D 	= models.CharField(max_length=1000)
	correct_op 	= models.CharField(choices=choice,max_length=10)
	mark 		= models.IntegerField(default=1)
	class Meta:
		verbose_name="问题"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.statments


class Exam(models.Model):
	subject = models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	name = models.CharField(max_length=100,blank=True,verbose_name="本次考试名称")
	class Meta:
		verbose_name="考试"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.name

class Mark(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name="用户")
	subject = models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	exam = models.ForeignKey(Exam,on_delete=models.CASCADE,verbose_name="考试名称")
	total_marks = models.CharField(max_length=10,verbose_name="总分数")
	class Meta:
		verbose_name="分数"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.total_marks

# # class Rank(models.Model):
# # 	pass


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这里是考试界面的首页title>
head>
<body>
<form action="" method="post">
    {% csrf_token %}
    <tr>
        <td>第一题,请选择答案td><br>
        <td>A:这是A选项td><br>
        <td>B:这是B选项td><br>
        <td>C:这是C选项td><br>
        <td>D:这是D选项td><br>
    tr>
    <tr>
        <td><input type="radio" name="question" value="A">Atd>
        <td><input type="radio" name="question" value="B">Btd>
        <td><input type="radio" name="question" value="C">Ctd>
        <td><input type="radio" name="question" value="D">Dtd>
        <br>
    tr>
    <tr>
        <td>第二题,请选择答案td><br>
        <td>A:这是A选项td><br>
        <td>B:这是B选项td><br>
        <td>C:这是C选项td><br>
        <td>D:这是D选项td><br>
    tr>
    <tr>
        <td><input type="radio" name="question2" value="A">Atd>
        <td><input type="radio" name="question2" value="B">Btd>
        <td><input type="radio" name="question2" value="C">Ctd>
        <td><input type="radio" name="question2" value="D">Dtd>
    tr><br>
        <tr>
        <td>{{ question_statements }}td><br>
        <td>{{ op_A }}td><br>
        <td>{{ op_B }}td><br>
        <td>{{ op_C }}td><br>
        <td>{{ op_D }}td><br>
    tr>
    <tr>
        <td><input type="radio" name="question_id" value="A">Atd>
        <td><input type="radio" name="question_id" value="B">Btd>
        <td><input type="radio" name="question_id" value="C">Ctd>
        <td><input type="radio" name="question_id" value="D">Dtd>
        <br>
    tr>
    <input type="submit" name="exam" value="提交">
form>
body>
html>

效果

计算分数算法_第1张图片

存在的问题是:不能循环遍历出同一科目或者同一考试下的所有题目

时间:2020年4月22日17:01:00
前端已经实现循环遍历,实现方法为:

#views.py
from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse
from .models import Question,Exam,Subject
from django.db import models
# Create your views here.
class ExamView(View):
    def get(self,request):
        subjetcs1=Subject.objects.get(id=1)
        print(subjetcs1)
        question=Question.objects.filter(subject=subjetcs1)
        context={
            "question":question,
        }
        return render(request, "exam.html",context=context)
    def post(self,request):
        question = request.POST.get('question')
        question2=request.POST.get('question2')
        options_list=re(request)
        print(question)
        print(question2)
        my_answer=[]
        my_answer.append(question)
        my_answer.append(question2)
        print(my_answer)
        print(options_list)
        return HttpResponse("success")
#打印所有题目的正确答案
def re(request):
    option_list = []
    options=Question.objects.all()
    for option in options:
        option_list.append(option.correct_op)
    return option_list

#models.py
from django.db import models
from users.models import User

# Create your models here.

#科目
class Subject(models.Model):
	name 			= models.CharField(max_length=255,verbose_name="科目名称")
	description 	= models.TextField(verbose_name="科目描述")
	cover_image 	= models.ImageField(upload_to = 'cover/',blank=True)
	class Meta:
		verbose_name="科目"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.name

class Question(models.Model):
	choice = (('A','A'),('B','B'),('C','C'),('D','D'))
	subject 	= models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	statments 	= models.TextField(verbose_name="问题描述")
	op_A 	= models.CharField(max_length=1000)
	op_B 	= models.CharField(max_length=1000)
	op_C 	= models.CharField(max_length=1000)
	op_D 	= models.CharField(max_length=1000)
	correct_op 	= models.CharField(choices=choice,max_length=10)
	mark 		= models.IntegerField(default=1)
	class Meta:
		verbose_name="问题"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.statments


class Exam(models.Model):
	subject = models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	name = models.CharField(max_length=100,blank=True,verbose_name="本次考试名称")
	class Meta:
		verbose_name="考试"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.name

class Mark(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name="用户")
	subject = models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	exam = models.ForeignKey(Exam,on_delete=models.CASCADE,verbose_name="考试名称")
	total_marks = models.CharField(max_length=10,verbose_name="总分数")
	class Meta:
		verbose_name="分数"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.total_marks

#exam.html
<form action="" method="post">
    {% csrf_token %}
{#循环读出所有的试题#}
    {% for foo in question %}
       <tr>
        <td>{{ foo.statments}}td><br>
        <td>{{ foo.op_A }}td><br>
        <td>{{ foo.op_B }}td><br>
        <td>{{ foo.op_C }}td><br>
        <td>{{ foo.op_D }}td><br>
    tr>
    <tr>
        <td><input type="radio" name="{{ foo.id }}" value="A">Atd>
        <td><input type="radio" name="{{ foo.id }}" value="B">Btd>
        <td><input type="radio" name="{{ foo.id }}" value="C">Ctd>
        <td><input type="radio" name="{{ foo.id }}" value="D">Dtd>
        <br>
    tr>
    {% endfor %}
    <input type="submit" name="exam" value="提交">
form>

时间:2020年4月22日17:45:31
实现了自动取出题目,答题,自动计算分数

#views.py
from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse
from .models import Question,Exam,Subject
from django.db import models
# Create your views here.
class ExamView(View):
    def get(self,request):
        #获取id=1的科目
        subjetcs1=Subject.objects.get(id=1)
        #打印这个科目的名称,可省去
        print(subjetcs1)
        #获取到这个科目下的所有试题
        question=Question.objects.filter(subject=subjetcs1)
        context={
            "question":question,
        }
        return render(request, "exam.html",context=context)
    def post(self,request):
        #设置用户答案为一个空列表
        my_answer = []
        #依次从前端的答案中读取用户的选择,一共3道题,所以,这里的4应该手动修改
        for i in range(1,4):
            answer1 = request.POST.get('%s'%i)
            my_answer.append(answer1)
            i+=1
        print("用户答案为:",my_answer)
        # 打印所有题目的正确答案
        correct_answer=re(request)
        print("正确答案为:",correct_answer)
        #基础,提升,专业分数,总分数均初始化为0
        base_mark=0
        promote_mark=0
        major_mark=0
        total_mark=0
        i=0
        while i <= 2:
            if i <= 0:
                if correct_answer[i] == my_answer[i]:
                    base_mark += 1
                    total_mark = total_mark + 1
            elif i <= 1:
                if correct_answer[i] == my_answer[i]:
                    total_mark = total_mark + 1
                    promote_mark += 1
            else:
                if correct_answer[i] == my_answer[i]:
                    total_mark = total_mark + 1
                    major_mark += 1
            i = i + 1
        print("总分数为:", total_mark)
        print("基础分数为:", base_mark)
        print("提升分数为:", promote_mark)
        print("专业分数为:", major_mark)
        return HttpResponse("success")
#打印所有题目的正确答案
def re(request):
    option_list = []
    options=Question.objects.all()
    for option in options:
        option_list.append(option.correct_op)
    return option_list

#models.py
from django.db import models
from users.models import User

# Create your models here.

#科目
class Subject(models.Model):
	name 			= models.CharField(max_length=255,verbose_name="科目名称")
	description 	= models.TextField(verbose_name="科目描述")
	cover_image 	= models.ImageField(upload_to = 'cover/',blank=True)
	class Meta:
		verbose_name="科目"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.name

class Question(models.Model):
	choice = (('A','A'),('B','B'),('C','C'),('D','D'))
	subject 	= models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	statments 	= models.TextField(verbose_name="问题描述")
	op_A 	= models.CharField(max_length=1000)
	op_B 	= models.CharField(max_length=1000)
	op_C 	= models.CharField(max_length=1000)
	op_D 	= models.CharField(max_length=1000)
	correct_op 	= models.CharField(choices=choice,max_length=10)
	mark 		= models.IntegerField(default=1)
	class Meta:
		verbose_name="问题"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.statments


class Exam(models.Model):
	subject = models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	name = models.CharField(max_length=100,blank=True,verbose_name="本次考试名称")
	class Meta:
		verbose_name="考试"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.name

class Mark(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name="用户")
	subject = models.ForeignKey(Subject,on_delete=models.CASCADE,verbose_name="考试科目")
	exam = models.ForeignKey(Exam,on_delete=models.CASCADE,verbose_name="考试名称")
	total_marks = models.CharField(max_length=10,verbose_name="总分数")
	class Meta:
		verbose_name="分数"
		verbose_name_plural=verbose_name
	def __str__(self):
		return self.total_marks
<form action="" method="post">
{#    csrf_token保护#}
    {% csrf_token %}
    {#循环读出所有的试题#}
    {% for foo in question %}
       <tr>
        {#每一道题的描述#}
        <td>{{ foo.statments}}td><br>
        <td>{{ foo.op_A }}td><br>
        <td>{{ foo.op_B }}td><br>
        <td>{{ foo.op_C }}td><br>
        <td>{{ foo.op_D }}td><br>
    tr>
    <tr>
        <td><input type="radio" name="{{ foo.id }}" value="A">Atd>
        <td><input type="radio" name="{{ foo.id }}" value="B">Btd>
        <td><input type="radio" name="{{ foo.id }}" value="C">Ctd>
        <td><input type="radio" name="{{ foo.id }}" value="D">Dtd>
        <br>
    tr>
    {% endfor %}
    <input type="submit" name="exam" value="提交">
form>

实现效果:
计算分数算法_第2张图片
计算分数算法_第3张图片

你可能感兴趣的:(Django)