Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Creating a score based on correct answers

Hi all,

This may be out of the scope of this forum, but thought I would check. I have a survey that has 10 questions and I want to compute a final score at the end of the survey.  I need to start at 0 and add 1 for each correct answer, then return the sum. I am looking through the help menu, trying to figure out how to do it in Perl, but no luck yet.  Any help is greatly appreciated.

Thank you!
asked Dec 21, 2016 by Ron H.

1 Answer

+1 vote
Hi Ron,

Do you want to program something like this:

https://demo.sawtoothsoftware.com/quiz/cgi-bin/ciwweb.pl?studyname=quiz

You can create free format variable and return 1 or 0 basis the answer. On the last page, you can sum all free format variables to get final score.

Regards,
Saurabh
answered Dec 21, 2016 by Saurabh Aggarwal Gold (27,605 points)
Perfect! Is there a place I can download that demo?
You can also use something like this too ...

It is a simplified example but it displays how each correct question (Q1 / Q2 / Q3) is given a score of 1 and totalled up at the end (TotalScore).

Define Q1Score, Q2Score, Q3Score and TotalScore as whole number pass-in fields. Pop this script into the footer of an appropriate question.
[%Begin Unverified Perl 

 if ((VALUE("Q1")==1))
  {
   SETVALUE("Q1Score",1); 
  }

 if ((VALUE("Q2")==5))
  {
   SETVALUE("Q2Score",1); 
  }

 if ((VALUE("Q3")==10))
  {
   SETVALUE("Q3Score",1); 
  }

 SETVALUE("TotalScore",Q1Score+Q2Score+Q3Score);
 
End Unverified%]
Hi Paul,
I tried using that code but am getting a script error when I hit the last page. When I check the error log I get a series of errors like the following, for each of the pass-in fields.

Bareword "Q2Score" not allowed while "strict subs" in use at (eval 58) line 54,
Try this Ron ...

I dived into it without putting the required thought into it. This script works. I just tested it.

Make sure you define Q1Score, Q2Score, Q3Score and TotalScore as whole number pass-in fields.
[%Begin Unverified Perl 
 
 if (VALUE("Q1")==5)
  {
   SETVALUE("Q1Score",1); 
  }
 else
  {
   SETVALUE("Q1Score",0); 
  }
 
 if (VALUE("Q2")==3)
  {
   SETVALUE("Q2Score",1); 
  }
 else
  {
   SETVALUE("Q2Score",0); 
  }

 if (VALUE("Q3")==1)
  {
   SETVALUE("Q3Score",1); 
  }
 else
  {
   SETVALUE("Q3Score",0); 
  }
 
 SETVALUE("TotalScore",GETVALUE("Q1Score")+GETVALUE("Q2Score")+GETVALUE("Q3Score"));
  
End Unverified%]

This script can be popped into the footer of a question after all scoring questions have been answered.
Fantastic! Thank you!!!
This code will work, but instead of a series of If/Else lines, I recommend the following:

1. Declare all your dummy variables equal to zero prior to the check for correct answers.
2. Check for correct answers only. No need to revise.

It will keep your lines of code to a minimum (which is always a good thing).
And using a for loop would be a better way too. Thanks Corey for chipping in.
...