+1 vote

I have a program where I ask if the user speaks French, Spanish or German. I am calculating the total number of foreign languages they speak. When I look at the CSV file I see that some users speak 4 languages! But this is not possible because I am just asking about three languages. What is wrong in my code?

*settings
	*back: yes

>>languagesSpoken = 0	
*question: Do you speak French?
	Yes
		>>languagesSpoken = languagesSpoken + 1
	No
	
*question: Do you speak Spanish?
	Yes
		>>languagesSpoken = languagesSpoken + 1
	No
	
*question: Do you speak German?
	Yes
		>>languagesSpoken = languagesSpoken + 1
	No
by (130 points)

1 Answer

+1 vote

The problem is that you are allowing the users to go back in your program. If they answer "yes" to the first two questions and then go back to the first one to change the answer to "no", the variable "languagesSpoken" is still 2. Then they answer yes to speaking Spanish and German, so the final value of the variable is 4.

You can try this code instead:

*settings
	*back: yes
*question: Do you speak French?
	Yes
		>>speaksFrench = 1
	No
		>>speaksFrench = 0
	
*question: Do you speak Spanish?
	Yes
		>>speaksSpanish = 1
	No
		>>speaksSpanish = 0
	
*question: Do you speak German?
	Yes
		>>speaksGerman = 1
	No
		>>speaksGerman = 0

>>languagesSpoken = speaksFrench + speaksSpanish + speaksGerman	
by (770 points)
Welcome to Guidedtrack Q&A, where you can ask questions and receive answers from other members of the community.
104 questions
112 answers
50 comments
168 users