Computing Result on server side but session data n

2019-03-05 06:37发布

Working on a web app where user takes test and they see their score at the end of the test. I compute the user score on the server side. The problem is that, when a user take a test, it affect the score of another user taking another test. what i mean is if a user A takes a test, and a user B is taking a test also from another system, at the end of the day, user A sees user B result as his result. Really do not know what is wrong with the code. But below is my code for computing the user score

def Compute_UserScore(self, details, ques_no):
    try:
        if(HomePage.answer_.strip() == ""):
            self.response.write("""<script type = "text/javascript">
            alert("Dear User, You can not answer same answer twice.. Take test Again !");
            </script>""")
            self.redirect('/otherPages/subjectSelect.html')
        else:
            count = 0
            HomePage.ans_no = 0
            HomePage.unans_no = 0
            HomePage.correct_no = 0
            HomePage.wrong_no = 0
            HomePage.failed_ques = list()
            HomePage.answer_ = HomePage.answer_.strip()
            question_1 = HomePage.question_.split(" gcdc_split_format ")
            while (count != (ques_no)):
                user_answer = str(details[count]).strip().capitalize()
                real_answer = str(HomePage.answer_[count]).strip().capitalize()
                if (len(str(user_answer).strip()) == 1):
                    HomePage.ans_no = HomePage.ans_no + 1
                    if(user_answer.strip() == real_answer.strip()):
                        HomePage.correct_no = HomePage.correct_no + 1
                    else:
                        HomePage.wrong_no = HomePage.wrong_no + 1
                        HomePage.failed_ques.append(str("No. " + str(int((count + 1))) + "  " + str(question_1[count])))
                else:
                    HomePage.unans_no = HomePage.unans_no + 1
                count = count + 1
            HomePage.answer_ = ""
    except:
        self.redirect('/')
    return " "

3条回答
一夜七次
2楼-- · 2019-03-05 06:40

The attribute values are shared for HomePage and are getting written over each other. Instead you should create an instance of the class.

查看更多
倾城 Initia
3楼-- · 2019-03-05 06:46

Not sure what your HomePage is but it seems to be a global variable or a package name. Either one means that every user of your web app is sharing the same memory storage (variables) for test result.

This is not the right way to do it if you want to save all users' test results. You should have a way to identify different users (i.e. having a user sign in) so you can show different results for a different user, and save the results in a database or in a python dictionary.

Another way to do it is to save the results in cookie, the cookie data is saved on the client side, so it would be different for different users.

查看更多
狗以群分
4楼-- · 2019-03-05 07:06

To prevent people from seeing other people's answer, you'd need an identification system. A simple username/password combination will do. You can then make a simple DB table to store this, along with their scores. You didn't put Homepage up for us to see, so I couldn't tell how you instantiated it, or what the structure looks like, but you need an ID system.

查看更多
登录 后发表回答