Computer Science Canada

Third question won't work

Author:  qmanjr5 [ Thu Dec 03, 2009 9:43 am ]
Post subject:  Third question won't work

Turing:

%Variables
var score : int
var answer1 : string
var answer2 : string
var answer3 : string

%variable values
score := 0
answer1 := "2"
answer2 := "33,000,000"
answer3 := "Barack Obama"

%Procedures
procedure correct
    score += 1
    put "YAY! :) You got it right. Your score has been increased by 1"
end correct
procedure incorrect
    score -= 1
    put "Oh no :( You got it wrong. Your score has been decreased by 1"
end incorrect
procedure currentscore
    put "Your current score is ", score, ". Next question."
end currentscore
procedure finished
    put "CONGRATULATIONS! You've finished. Your final score was ", score, ". Good job."
end finished

%main code
put "Hello, welcome to my quiz."
put "First question"
put "What is 1+1?"
get answer1
if answer1 = "2" then
    correct
else
    incorrect
end if
currentscore
put "Question two."
put "What is the population of Canada?"
get answer2
if answer2 = "33,000,000" then
    correct
elsif answer2 = "thirty three million" then
    correct
elsif answer2 = "33million" then
    correct
elsif answer2 = "33 million" then
    correct
elsif answer2 = "33000000" then
    correct
else
    incorrect
end if
currentscore
put "Question 3"
put "Who is the president of the united states of america?"
get answer3
if answer3 = "Barack Obama" then
    correct
elsif answer3 = "barack obama" then
    correct
else
    incorrect
end if


that's my code at the moment.

If you run that, you should be able to get the first two questions, no problem. But when you get to the 3rd question, it'll say you got it wrong.

Or maybe it's just my Turing...?

Any help will be appreciated

BY THE WAY: Don't say anything about my code, please. I know it's not the best

Author:  apomb [ Thu Dec 03, 2009 10:59 am ]
Post subject:  RE:Third question won\'t work

I understand why you set your score to zero, but why do you initialize your answers when you just overwrite them with the user's input?

The last question does not work because of spaces. Also, the same problem arises with the second question if you try entering "thirty three million" as the answer.

Author:  DemonWasp [ Thu Dec 03, 2009 11:41 am ]
Post subject:  RE:Third question won\'t work

Your get calls are only getting the first "token" from your user's input. That is, if I enter the answer:

Barack Obama

then the variable you get back has "Barack" and the next time you get something, it will have the value "Obama". To fix this, you want to grab the whole line of input:

code:

get : my_variable : *


The :* on the end tells it to take all the input up to an <Enter> key.

Author:  qmanjr5 [ Thu Dec 03, 2009 8:26 pm ]
Post subject:  RE:Third question won\'t work

So
Turing:

get answer3 *


By the way, apomb, would do I do to fix that? Just not set the variables any values?

I believe I need to set the score to 0, don't I?

Author:  Zren [ Thu Dec 03, 2009 9:03 pm ]
Post subject:  RE:Third question won\'t work

code:
answer1 = Correct Answer
Ask Question
Get answer1 %Overwrites Correct Answer with User Input
If answer1 = Correct Answer -> Score + 1
Else -> Score - 1


It's because your using answer1, answer2, answer3 as the same variables for user input, not for the correct answer to the questions.

BTW, You don't need a new variable for each answer. Since it should be like

code:
Ask Question
Get User's Input
If User's Input = Correct Answer -> Score + 1
Else -> Score - 1


So you could use the same variable for the user's answer on each question. Since you don't need to remember the answers given for all the questions (or you might I dunno).

Author:  apomb [ Fri Dec 04, 2009 8:41 am ]
Post subject:  Re: RE:Third question won\'t work

qmanjr5 @ Thu Dec 03, 2009 8:26 pm wrote:
So
Turing:

get answer3 *


By the way, apomb, would do I do to fix that? Just not set the variables any values?

I believe I need to set the score to 0, don't I?


you should definitely set score to 0 yes, but the other variables are just overkill.
like zren said, you need only one "answer" variable, and reuse it for each question, and as for getting input, follow demonwasp's example exactly. (you forgot some important punctuation)

Author:  TheGuardian001 [ Fri Dec 04, 2009 9:04 am ]
Post subject:  Re: Third question won't work

On that note, only the second : was needed. The first is used to indicate getting information from a file.

code:

get my_var :*


In Turing, the : after the variable indicates that you want to read a certain number of characters, whatever comes after defines how many. * is used to read until the end of a line.

Author:  qmanjr5 [ Fri Dec 04, 2009 11:27 am ]
Post subject:  RE:Third question won\'t work

So, I DON'T need the
answer1 = "2"
answer2 = "33,000,000"
etc

Could I do this?

Turing:

var answer1 : string
answer1 = "2"
get answer11
if answer11 = answer1 then correct


And so I just put, for question 3,
[syntax='turing"]
get answer3
if answer3 = "barack obama" then correct
[/syntax]

Author:  apomb [ Fri Dec 04, 2009 11:34 am ]
Post subject:  RE:Third question won\'t work

you should really use the same concepts for each of your questions.
i.e.
Turing:

var answer : string
get answer :*
if answer = "2" then correct

get answer :*
if answer = to_upper_case(thirty three million) or answer = 33,000,000 or answer = 33000000 or answer = 33 million then correct

get answer :*
if answer = to_upper_case(barack obama) then correct


That is not actual turing but its close enough you should see what i mean when i say "the same concepts for each question"

Author:  qmanjr5 [ Fri Dec 04, 2009 3:37 pm ]
Post subject:  RE:Third question won\'t work

D: I'm so confused Sad

I'll figure it out D:

'Thanks all, problem solved for now Smile


: