Computer Science Canada

Boolean and if statement help

Author:  sammi [ Sat Oct 15, 2011 4:56 pm ]
Post subject:  Boolean and if statement help

What is it you are trying to achieve?
<Im making a magic 8 ball for a project and im trying make sure that people cant just put in a bunch of numbers (i.e "405968245") and get an answer>


What is the problem you are having?
<there is an error message popping up every time i hit run and it says my "if statement" must be boolean and im not even sure if the code in my if statement is right.>


Describe what you have tried to solve this problem
<a bunch of stuff>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<var Question : string
var words : array 1 .. 11 of string

% parts of the array
words (1) := "yes"
words (2) := "no"
words (3) := "maybe"
words (4) := "thats for me to know and you to find out Smile"
words (5) := "of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absulutely"

locatexy (200,390)% where "ask a question" turns up on the screen

Draw.FillOval(330,250,100,100, brightred)% drawing the ball

var Answers : int

put "Ask the Magic 8-Ball a question"
locatexy (250,250)% where the typing cursor is

loop

% make the person put in words, not numbers
if Question := int then
put "thats not a question"
elsif
get Question : *
locatexy (200,100)% where the answer will show up on the screen



% put any one of my answers on the screen
Answers := Rand.Int (1,11)
put words (Answers)
locatexy (320,250)% reset the cursor

end loop
delay(20)

>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<Answer Here>

Author:  Tony [ Sat Oct 15, 2011 5:24 pm ]
Post subject:  Re: Boolean and if statement help

sammi @ Sat Oct 15, 2011 4:56 pm wrote:
im not even sure if the code in my if statement is right.

It's not.
code:

if Question := int then

That's... not how things work. You are trying to assign ( := operator) a type int as a value to variable Question. Even if you could somehow compare types, this would never make sense, since
code:

var Question : string

Question is always a string. "1234" (in quotes) is still a string.

Author:  sammi [ Sat Oct 15, 2011 6:04 pm ]
Post subject:  RE:Boolean and if statement help

then how do i fix it?

Author:  Tony [ Sat Oct 15, 2011 6:10 pm ]
Post subject:  RE:Boolean and if statement help

well, what exactly do you want to check for here?

Author:  Aange10 [ Sat Oct 15, 2011 6:28 pm ]
Post subject:  Re: Boolean and if statement help

Your coding isn't correct. But I can help you with your project. First and Foremost, you need to read the Turing Walkthrough. It will give you a nice understand of Turing. Secondly, you're looking at string manipulation (scary, i know. But it's easy(: ) to solve your problem. That can be found under the Turing Walkthrough, or directly from this link: http://compsci.ca/v3/viewtopic.php?t=8229.

Now, on to your project.

sammi wrote:

<there is an error message popping up every time i hit run and it says my "if statement" must be boolean and im not even sure if the code in my if statement is right.>


Turing is giving you an error because the if statement must come out with one of two answers. True or False. So the problem lies within your statement. As tony said, ':=' means define.
The other problem with your if statement is that Question is already defined as a string.
code:

    var Question : string
 

So there is no way Question is an integer, if it's a string. That's why that doesn't work. However, there is a way Question can contain an integer. Finding how how to do that can be found in the second link I gave you; String Manipulation.

However, you're in luck as I'm willing to give you a hand with your coding Smile. So lets start, shall we?
First and foremost we need to define our variables.
Turing:

var Question : string
var Answer : int
var valid : boolean := true
var words : array 1 .. 11 of string
words (1) := "yes"
words (2) := "no"
words (3) := "maybe"
words (4) := "thats for me to know and you to find out Smile"
words (5) := "of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absulutely"

Pretty straight forward. Incase your wondering, var valid : boolean := true is a quick way of saying
code:

var valid : boolean
valid := true

(A boolean variable holds one of two values; True or False.)

Now that we have our variables, we'll want to get our nice 8 ball out there, and get some magic questions going!
Turing:

% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)
% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"
% Getting a response
locatexy (250, 250)
get Question : *


So this is pretty straight forward. We drew the ball, output a phrase to the screen, and we asked for a response. Now comes the tricky part, String Manipulation.

Turing:

% Make sure the question contains no numbers
for i : 1 .. length (Question) % This for is Representing the Character of the string
    for b : 1 .. 9 % This for checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false
            exit
        end if
    end for
    if valid then
    else
        cls
        put "Numbers found! Please do not use numbers."
        delay (2000)
        exit
    end if
end for


I know what your thinking... Yeah, that's a lot of new things. Lets dissect it piece by piece. Hopefully you know fors, if not I recommend you read through the Turing Walkthrough a bit.

code:

for i : 1 .. length (Question) % This for is Representing the Character of the string
    for b : 1 .. 9 % This for checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.


If you don't know what a for is, all it is is a loop that only loops a number of times [opposed to indefinitely]. Here, the for i starts at 1 and runs through the entire length of the string Question. And for b runs through 1-9. The important thing, now, is our if statement.

code:

if Question (i) = intstr(b) then


This here is saying if the string Question position number (i) is = to (b) then. The instr is there just so that we can change b to a string. So that it may look through Question (also a string!).
Now the next part is to make sure that if that statement becomes true (i.e we found a number in our string) that it does something.
Turing:

if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false
            exit
        end if
    end for
    if valid then
    else
        cls
        put "Numbers found! Please do not use numbers."
        delay (2000)
        exit
    end if
end for


Now this here says that if the statement is true, then it sets our valid variable to false. If you look after that, it says that if valid is not true, then it gives them the error message and exits the for.
If they did follow the rules, and no integer was inputed, then it continues past the for, to our last piece of code.

Turing:

if valid then
    locatexy (200, 100)
    Answer := Rand.Int (1, 11)
    put words (Answer)
end if


Our last bit here says that if the question is valid then it randomly selects an answer, and puts it on the screen.
So here is the code all together.
Turing:

var Question : string
var Answer : int
var valid : boolean := true
var words : array 1 .. 11 of string
words (1) := "yes"
words (2) := "no"
words (3) := "maybe"
words (4) := "thats for me to know and you to find out Smile"
words (5) := "of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absulutely"
%% Program
% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)
% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"
% Getting a response
locatexy (250, 250)
get Question : *
% Make sure the question contains no numbers
for i : 1 .. length (Question) % This for is Representing the Character of the string
    for b : 1 .. 9 % This for checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false
            exit
        end if
    end for
    if valid then
    else
        cls
        put "Numbers found! Please do not use numbers."
        delay (2000)
        exit
    end if
end for
if valid then
    locatexy (200, 100)
    Answer := Rand.Int (1, 11)
    put words (Answer)
end if


*suggestion* You should add a way to ask the user if they'd like to repeat (:

Author:  sammi [ Sat Oct 15, 2011 6:31 pm ]
Post subject:  RE:Boolean and if statement help

nvr mnd scrap the numbers thing.

how can i assign certain answers to come up when a certain kind of question is asked like:
Q: will i ever become rich?
A: I doubt it

Author:  sammi [ Sat Oct 15, 2011 6:35 pm ]
Post subject:  RE:Boolean and if statement help

omg thank you so much!!!!!!
sorry i posted that last reply " nvr mnd scrap the numbers thing" without refreshing the page and i didnt see ur post srry bout that.

thanks again!!!!!

Author:  Aange10 [ Sat Oct 15, 2011 6:36 pm ]
Post subject:  Re: Boolean and if statement help

sammi wrote:

nvr mnd scrap the numbers thing.

how can i assign certain answers to come up when a certain kind of question is asked like
Q will i ever become rich?
A I doubt it


Well first off, reading the Turing Walkthrough would teach you everything you need to know. But to answer your question... Tell me, what do you want to happen?


EDIT: No problem(:

Author:  Tony [ Sat Oct 15, 2011 6:41 pm ]
Post subject:  RE:Boolean and if statement help

You could see is a question is an exact match to something that you have thought of, but you are unlikely to hit those. E.g.

"Will I become <adjective> rich in the next <N> <time-units>?"

http://en.wikipedia.org/wiki/Natural_language_processing is hard. It is really really really hard.

But you can approximate this with checking if a particular word(s) are present in the question. It'd be more interesting if you gave a custom answer too, but that's up to you.

Q: anything that includes word "rich"
A: I'm not a certified financial advisor.

index is the function that you'll be looking for.

Author:  sammi [ Sat Oct 15, 2011 6:45 pm ]
Post subject:  RE:Boolean and if statement help

haha im not a certified financial advisor i pretty clever.
i'll take a look at the links but BTW i may be back ......
thanks again,
sammi

Author:  sammi [ Sat Oct 15, 2011 6:46 pm ]
Post subject:  RE:Boolean and if statement help

is pretty clever*
sorry lol

Author:  Aange10 [ Sat Oct 15, 2011 6:52 pm ]
Post subject:  RE:Boolean and if statement help

Just edit your posts, Sammi, instead of making new ones.

Author:  sammi [ Sat Oct 15, 2011 6:53 pm ]
Post subject:  RE:Boolean and if statement help

i didnt even know i could edit them.... oops
im new to this site

Author:  sammi [ Sat Oct 15, 2011 6:57 pm ]
Post subject:  RE:Boolean and if statement help

alright im already back lol

i understand the index thing but i dont know how to intergrate it into my existing code

Author:  Tony [ Sat Oct 15, 2011 6:59 pm ]
Post subject:  RE:Boolean and if statement help

if-statements
loops
reading-from-a-file (will make your life so much easier)
arrays (optional. reading from a file every time is slow)

Author:  Aange10 [ Sat Oct 15, 2011 6:59 pm ]
Post subject:  RE:Boolean and if statement help

No problem(: ... If you want to learn about Turing then i suggest you read the walkthrough. It really will tell you everything. But if you get confused, you can make as many threads here as you like. As long as they remain relevant to the thread topic. And of course follow the rules(:

----> Turing Walkthrough <----
A.K.A : Your new best friend

Author:  sammi [ Sat Oct 15, 2011 7:05 pm ]
Post subject:  RE:Boolean and if statement help

i already know what some of the different codes do i just dont know how to put them into my program.
seeing them by themselves and putting them into my code are 2 completetly different things to me

Author:  Aange10 [ Sat Oct 15, 2011 7:45 pm ]
Post subject:  RE:Boolean and if statement help

Ahh, I see. But thankfully, the Turing Walkthrough also has examples, and at the end of the tutorials have "questions" that you can do. Mostly to make sure you understand the content (with the answers at the bottom)

Author:  Insectoid [ Sat Oct 15, 2011 8:18 pm ]
Post subject:  RE:Boolean and if statement help

The Turing Walkthrough won't teach you how to program. It may help you answer common, general questions but it doesn't cover everything. Aange10, you have like 5 posts just advocating the walkthrough. That's borderline spam.

Author:  sammi [ Sat Oct 15, 2011 8:28 pm ]
Post subject:  RE:Boolean and if statement help

the examples it has are different from my program though

Author:  sammi [ Sat Oct 15, 2011 8:29 pm ]
Post subject:  Re: RE:Boolean and if statement help

Insectoid @ Sat Oct 15, 2011 8:18 pm wrote:
The Turing Walkthrough won't teach you how to program. It may help you answer common, general questions but it doesn't cover everything. Aange10, you have like 5 posts just advocating the walkthrough. That's borderline spam.


thank you

Author:  Raknarg [ Sat Oct 15, 2011 8:31 pm ]
Post subject:  RE:Boolean and if statement help

That is the idea of Computer Science. You are given tools and ideas to solve a problem, rather than steps and prodedures. You can take the examples and implement their ideas into your own code.

Author:  Tony [ Sat Oct 15, 2011 8:31 pm ]
Post subject:  RE:Boolean and if statement help

well it wouldn't be very interesting if everyone was just following the same instructions to build same things; never anything new.

Author:  sammi [ Sat Oct 15, 2011 9:10 pm ]
Post subject:  RE:Boolean and if statement help

ok so when im using the createbutton command it says i have to put the procedure before i declare the button variables. so should the procedure go at the very top right after "import GUI" and before the rest of my program variables?

Author:  Aange10 [ Sat Oct 15, 2011 9:10 pm ]
Post subject:  Re: RE:Boolean and if statement help

Insectoid @ 15/10/2011, 7:18 pm wrote:
The Turing Walkthrough won't teach you how to program. It may help you answer common, general questions but it doesn't cover everything. Aange10, you have like 5 posts just advocating the walkthrough. That's borderline spam.


I have to disagree, I'm learning how to do it, and the only thing I've had helping me are the people here, and the Turing Walkthrough. As Raknarg put it:
Raknarg wrote:

That is the idea of Computer Science. You are given tools and ideas to solve a problem, rather than steps and prodedures. You can take the examples and implement their ideas into your own code.


in my opinion, it does teach you to program [in turing]. It gives you the tools you need, and shows you examples of how to use them. The rest of programming is using your creativity to put them to life.

Author:  sammi [ Sat Oct 15, 2011 9:13 pm ]
Post subject:  Re: RE:Boolean and if statement help

Aange10 @ Sat Oct 15, 2011 9:10 pm wrote:
Insectoid @ 15/10/2011, 7:18 pm wrote:
The Turing Walkthrough won't teach you how to program. It may help you answer common, general questions but it doesn't cover everything. Aange10, you have like 5 posts just advocating the walkthrough. That's borderline spam.


I have to disagree, I'm learning how to do it, and the only thing I've had helping me are the people here, and the Turing Walkthrough. As Raknarg put it:
Raknarg wrote:

That is the idea of Computer Science. You are given tools and ideas to solve a problem, rather than steps and prodedures. You can take the examples and implement their ideas into your own code.


in my opinion, it does teach you to program [in turing]. It gives you the tools you need, and shows you examples of how to use them. The rest of programming is using your creativity to put them to life.


but everyone learns differently

Author:  Aange10 [ Sat Oct 15, 2011 9:15 pm ]
Post subject:  RE:Boolean and if statement help

Which is why it's my opinon. But we're going off topic. What error are you getting with your GUI?

Author:  sammi [ Sat Oct 15, 2011 9:16 pm ]
Post subject:  RE:Boolean and if statement help

some people like manuals and some people (like me) like people sitting down with them, explaining things to them while they do it

Author:  sammi [ Sat Oct 15, 2011 9:16 pm ]
Post subject:  RE:Boolean and if statement help

its not an error, i just dont want to put my code in the wrong order and get a bad mark because of it

Author:  Tony [ Sat Oct 15, 2011 9:17 pm ]
Post subject:  Re: RE:Boolean and if statement help

sammi @ Sat Oct 15, 2011 9:10 pm wrote:
so should the procedure go at the very top right after "import GUI" and before the rest of my program variables?

"declaration before use" -- basically a variable/procedure must exist, before you use its name in the program. Anywhere above the first use is fine.

If you run into circular dependencies
code:

function A() : int
   return B()
end A

function B() : int
   return A()
end B

then you could use forward to resolve the issue.

Author:  sammi [ Sat Oct 15, 2011 9:24 pm ]
Post subject:  RE:Boolean and if statement help

oh ok thanks!!!!!!!!!!!

my teachers a bit picky about where you put the code but if he puts up a fuss then Ill just show him the Turing Walkthrough whatchyamacallit thingy lol Smile "aka my new best friend" haha

Author:  Tony [ Sat Oct 15, 2011 9:32 pm ]
Post subject:  RE:Boolean and if statement help

Well your code should be organized in some way that makes sense. As one of my profs said:
Quote:

If you write clean code, your TAs will not get as angry reading it, and will be less likely to give you bad grades.

Author:  sammi [ Sat Oct 15, 2011 9:34 pm ]
Post subject:  Re: RE:Boolean and if statement help

Tony @ Sat Oct 15, 2011 9:32 pm wrote:
Well your code should be organized in some way that makes sense. As one of my profs said:
Quote:

If you write clean code, your TAs will not get as angry reading it, and will be less likely to give you bad grades.



alrighty!!!!!

Author:  sammi [ Sat Oct 15, 2011 9:42 pm ]
Post subject:  Re: RE:Boolean and if statement help

Tony @ Sat Oct 15, 2011 9:32 pm wrote:
Well your code should be organized in some way that makes sense. As one of my profs said:
Quote:

If you write clean code, your TAs will not get as angry reading it, and will be less likely to give you bad grades.


so when the wallkthrough says:

var button1:int:=GUI.CreateButton(x,y,0,"Button Text",procedure)
var button2:int:=GUI.CreateButton(x,y,0,"Button Text",procedure)

do i replace "procedure" with my put statement from above?

Author:  Tony [ Sat Oct 15, 2011 9:44 pm ]
Post subject:  RE:Boolean and if statement help

when in doubt, check the documentation -- GUI.CreateButton

Author:  sammi [ Sun Oct 16, 2011 10:01 am ]
Post subject:  RE:Boolean and if statement help

my computer blocks the Turing reference thing so thats why i couldnt check that

Author:  Insectoid [ Sun Oct 16, 2011 11:25 am ]
Post subject:  RE:Boolean and if statement help

procedure is an actual procedure. If you don't know what a procedure is, you probably shouldn't be touching buttons yet. Stick to a text interface for now imo.

Author:  sammi [ Sun Oct 16, 2011 11:39 am ]
Post subject:  Re: RE:Boolean and if statement help

Insectoid @ Sun Oct 16, 2011 11:25 am wrote:
procedure is an actual procedure. If you don't know what a procedure is, you probably shouldn't be touching buttons yet. Stick to a text interface for now imo.


i know what a procedure is but i just wasnt sure if i had to sustitute it

Author:  Aange10 [ Sun Oct 16, 2011 11:42 am ]
Post subject:  RE:Boolean and if statement help

sammi wrote:

i know what a procedure is but i just wasnt sure if i had to sustitute it


Experiment

Author:  sammi [ Sun Oct 16, 2011 12:03 pm ]
Post subject:  RE:Boolean and if statement help

do you guys mind if i post my code again and ask you a few things?

Author:  sammi [ Sun Oct 16, 2011 12:07 pm ]
Post subject:  Re: RE:Boolean and if statement help

sammi @ Sun Oct 16, 2011 12:03 pm wrote:
do you guys mind if i post my code again and ask you a few things?


never mind, i figured it out

Author:  sammi [ Sun Oct 16, 2011 12:59 pm ]
Post subject:  RE:Boolean and if statement help

So here my code so far. it has a few problems (mentioned underneath the code)
do u mind helping me?

import GUI

%%% WHAT I WANT THE BUTTON TO DO WHEN I CLICK IT%%%
procedure NextQuestionMenu
put "Ask the magic 8 ball another question"
end NextQuestionMenu

procedure ExitGame
put " Awww you can't tell me your done already....."
end ExitGame

%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var Answer : int % part of the array
var valid : boolean := true %number check
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out Smile"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"

%% PROGRAM%%%%%
loop
% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"

% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)

% Getting a response
locatexy (250, 250)
get Question : *

% Make sure the question contains no numbers
for i : 1 .. length (Question) % Represents the Character of the string
for b : 1 .. 9 % checks to see if the character, i, has a number.
if Question (i) = intstr(b) then % If it does, it sets valid to false.
valid := false % if it finds a number then it recognizes an improper question
cls()
exit
end if
end for
end for

locatexy (200, 100) % position of answer and "invalid" comment

if valid then

Answer := Rand.Int (1, 11)
put words (Answer)

else

locatexy (200, 220)
put "Thats not a question silly!!!"
delay (1500)
cls()
end if


locatexy (200, 20)
put "Would you like to ask another question?"
var YesButton : int:= GUI.CreateButton (250, 50, 0, "Yes", NextQuestionMenu)
var NoButton : int:= GUI.CreateButton (350, 50, 0, "No", ExitGame)


exit when GUI.ProcessEvent
end loop


PROBLEMS: When they enter a non valid question (numbers), it does exactly what i want it to. But when they enter a valid question then the answer pops up but so does "thats not a question silly" which i only want for invalid questions.

Author:  Aange10 [ Sun Oct 16, 2011 2:10 pm ]
Post subject:  RE:Boolean and if statement help

To put your code in a syntax (Like we all did ours) use (syntax="turing") and (/syntax) to quit ... But don't use ()'s use []'s ... It makes it much much easier to read....

But, looking at your coding, there are a few things you need to do... If they ask an invalid question, what happens to our valid variable? It turns it false... When they ask their next question, did it reset the value?

Turing:

valid := true % Reset the question value


Secondly, you're drawling the buttons, forking the program, and then continuing back to the beginning. (Forking the program is where you make it do two things at once... sorta.) ... So the program never stops to wait for you to hit a button, it just draws them and continues on. The buttons "work", but not until your program gets back to them.

It's a bit complicated, but your solution would look like

Turing:

    put "Would you like to ask another question?"
    loop
        var YesButton : int := GUI.CreateButton (250, 50, 0, "Yes", NextQuestionMenu)
        var NoButton : int := GUI.CreateButton (350, 50, 0, "No", ExitGame)
        exit when GUI.ProcessEvent
    end loop


.... Now, you're program does exactly what you told it to do.

Author:  sammi [ Sun Oct 16, 2011 2:57 pm ]
Post subject:  RE:Boolean and if statement help

okay but when i click it it just draws the buttons over and over again

Author:  Aange10 [ Sun Oct 16, 2011 3:17 pm ]
Post subject:  Re: RE:Boolean and if statement help

sammi @ 16/10/2011, 1:57 pm wrote:
okay but when i click it it just draws the buttons over and over again


Heh, if you're going to make two posts about the same thing, at least read them both. http://compsci.ca/v3/viewtopic.php?p=244947#244947


: