
-----------------------------------
sammi
Sat Oct 15, 2011 4:56 pm

Boolean and if statement help
-----------------------------------
What is it you are trying to achieve?



What is the problem you are having?



Describe what you have tried to solve this problem



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)








Please specify what version of Turing you are using


-----------------------------------
Tony
Sat Oct 15, 2011 5:24 pm

Re: Boolean and if statement help
-----------------------------------
im not even sure if the code in my if statement is right.
It's not.
[code]
if Question := int then 
[/code]
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 
[/code]
Question is always a string. "1234" (in quotes) is still a string.

-----------------------------------
sammi
Sat Oct 15, 2011 6:04 pm

RE:Boolean and if statement help
-----------------------------------
then how do i fix it?

-----------------------------------
Tony
Sat Oct 15, 2011 6:10 pm

RE:Boolean and if statement help
-----------------------------------
well, what exactly do you want to check for here?

-----------------------------------
Aange10
Sat Oct 15, 2011 6:28 pm

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.


 


   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.
    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 :). So lets start, shall we?
  First and foremost we need to define our variables.
  
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

% 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.


% 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.

 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.

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.

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.


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.

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 (:

-----------------------------------
sammi
Sat Oct 15, 2011 6:31 pm

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

-----------------------------------
sammi
Sat Oct 15, 2011 6:35 pm

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!!!!!

-----------------------------------
Aange10
Sat Oct 15, 2011 6:36 pm

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


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(:

-----------------------------------
Tony
Sat Oct 15, 2011 6:41 pm

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  rich in the next  ?"

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.

[tdoc]index[/tdoc] is the function that you'll be looking for.

-----------------------------------
sammi
Sat Oct 15, 2011 6:45 pm

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

-----------------------------------
sammi
Sat Oct 15, 2011 6:46 pm

RE:Boolean and if statement help
-----------------------------------
is pretty clever*
sorry lol

-----------------------------------
Aange10
Sat Oct 15, 2011 6:52 pm

RE:Boolean and if statement help
-----------------------------------
Just edit your posts, Sammi, instead of making new ones.

-----------------------------------
sammi
Sat Oct 15, 2011 6:53 pm

RE:Boolean and if statement help
-----------------------------------
i didnt even know i could edit them.... oops
im new to this site

-----------------------------------
sammi
Sat Oct 15, 2011 6:57 pm

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

-----------------------------------
Tony
Sat Oct 15, 2011 6:59 pm

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)

-----------------------------------
Aange10
Sat Oct 15, 2011 6:59 pm

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 