Posted: 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
Please specify what version of Turing you are using
<Answer Here>
Sponsor Sponsor
Tony
Posted: 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.
Posted: 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 Questionis 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.
Turing:
var Question :string var Answer :int var valid :boolean:=true var words :array1.. 11ofstring
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 endif endfor if valid then else cls put"Numbers found! Please do not use numbers." delay(2000) exit endif endfor
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 endif endfor if valid then else cls put"Numbers found! Please do not use numbers." delay(2000) exit endif endfor
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) endif
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 :array1.. 11ofstring
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 endif endfor if valid then else cls put"Numbers found! Please do not use numbers." delay(2000) exit endif endfor if valid then locatexy(200, 100)
Answer := Rand.Int (1, 11) put words (Answer) endif
*suggestion* You should add a way to ask the user if they'd like to repeat (:
sammi
Posted: 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
sammi
Posted: 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!!!!!
Aange10
Posted: 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(:
Sponsor Sponsor
Tony
Posted: 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>?"
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.