Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » I need to separate words in an answer
- zacadoole1
-
100+ posts
I need to separate words in an answer
I am currently making an entirely text-based-game where you type into the project exactly what you want to do to progress through a series of levels, known as “Rooms”. However, there I've encountered a serious problem that I have had for a looooong time: there is no way to distinguish between the different words in the answer (user input)! It would be easy if there were a block like (word |1| of |world|) like the block (letter |1| of |world|), but alas, there is not. I have tried many different solutions, some from multiple different scratchers (via the suggestions website), but none of them work. Here is my current attempt:

Each item in the list “Input” is a different word the user typed (words being defined as groups of letters/numbers/symbols separated by spaces). This items in this list would be fed to scripts such as this one:

Now you may be asking yourself: “What is wrong with those scripts? They look like they should work to me!” Well let me clear that up for you: they don't. For some reason, the list “Input” never has anything in it and the variable “Response Distinguished” never gets set to true (is always set to false). Can anyone help me?
Edit 1: Formatting.
Edit 2: More formatting.
Edit 3: Clarification.
Edit 4: Grammar

Each item in the list “Input” is a different word the user typed (words being defined as groups of letters/numbers/symbols separated by spaces). This items in this list would be fed to scripts such as this one:

Now you may be asking yourself: “What is wrong with those scripts? They look like they should work to me!” Well let me clear that up for you: they don't. For some reason, the list “Input” never has anything in it and the variable “Response Distinguished” never gets set to true (is always set to false). Can anyone help me?
Edit 1: Formatting.
Edit 2: More formatting.
Edit 3: Clarification.
Edit 4: Grammar
Last edited by zacadoole1 (May 10, 2013 23:15:52)
- turkey3
-
1000+ posts
I need to separate words in an answer
Don't worry, there's an easy workaround.
delete (all) of (words)
insert () at (1) of (words) //this inserts a new, blank item onto the list
set (letter#) to (1)
repeat (length of (answer))
if <not <(letter (letter#) of (answer) = ( )>> //this checks if the letter is a space
replace item (1) of (words) with (join (item (1) of (words)) (letter (letter#) of (answer)))
else
insert () at (1) of (words) //this creates a new item (or word in this case) in the list
end
change by (1) //this changes it so the next letter will be added to the list
end
To sum it up, you take each letter, add it to item 1 of a list, and if the letter is a space it creates a new item or word into the list.
In this case, letter# is a variable and words is a list. In the end, you'll have words. I hope you understand it without the scratchblocks images. Try making it and visualizing it that way.
delete (all) of (words)
insert () at (1) of (words) //this inserts a new, blank item onto the list
set (letter#) to (1)
repeat (length of (answer))
if <not <(letter (letter#) of (answer) = ( )>> //this checks if the letter is a space
replace item (1) of (words) with (join (item (1) of (words)) (letter (letter#) of (answer)))
else
insert () at (1) of (words) //this creates a new item (or word in this case) in the list
end
change by (1) //this changes it so the next letter will be added to the list
end
To sum it up, you take each letter, add it to item 1 of a list, and if the letter is a space it creates a new item or word into the list.
In this case, letter# is a variable and words is a list. In the end, you'll have words. I hope you understand it without the scratchblocks images. Try making it and visualizing it that way.
Last edited by turkey3 (May 10, 2013 23:50:18)
- zacadoole1
-
100+ posts
I need to separate words in an answer
I've created your script word-for-word what you told me to do, and it's the same story; nothing gets added to the list “Input” (in your case “Words”), and the variable “Response Distinguished” never becomes true. Here is the script I made based on your script (your list “Words” is my list “Input” and your variable “letter#” is my variable “Letter Counter”): Don't worry, there's an easy workaround.
delete (all) of (words)
insert () at (1) of (words) //this inserts a new, blank item onto the list
set (letter#) to (1)
repeat (length of (answer))
if <not <(letter (letter#) of (answer) = ( )>> //this checks if the letter is a space
replace item (1) of (words) with (join (item (1) of (words)) (letter (letter#) of (answer)))
else
insert () at (1) of (words) //this creates a new item (or word in this case) in the list
end
change by (1) //this changes it so the next letter will be added to the list
end
To sum it up, you take each letter, add it to item 1 of a list, and if the letter is a space it creates a new item or word into the list.
In this case, letter# is a variable and words is a list. In the end, you'll have words. I hope you understand it without the scratchblocks images. Try making it and visualizing it that way.

Do you (or anyone else) have any idea what went wrong? (I'll still appreciate an entirely new script or any help with my original ones)
- BoltBait
-
1000+ posts
I need to separate words in an answer
You may want to review the scripts of this project:
http://scratch.mit.edu/projects/1516358
It is an adventure game with a fairly flexible input system.
http://scratch.mit.edu/projects/1516358
It is an adventure game with a fairly flexible input system.
- andre_rifaut
-
100+ posts
I need to separate words in an answer
The code you've put is simple and seems correct.
If I could test it I could tell you what happens.
I copied and modified a little bit your code, and it seems to work. See http://scratch.mit.edu/projects/10165988
(Could you tell me how you can insert images of the code into the messages ? I cannot do that.)
If I could test it I could tell you what happens.
I copied and modified a little bit your code, and it seems to work. See http://scratch.mit.edu/projects/10165988
(Could you tell me how you can insert images of the code into the messages ? I cannot do that.)
Last edited by andre_rifaut (May 11, 2013 12:37:54)
- zacadoole1
-
100+ posts
I need to separate words in an answer
http://scratch.mit.edu/projects/10165988Wow, that is amazingly functional. Now, I think I've come up with a script that will successfully separate the words. Here it is: I copied and modified a little bit your code, and it seems to work. See

And here is what the game screen looks like when I type in “hello world”:

Now I'm having problems with communicating the user input to the other scripts in the game. Does anyone think I should make a new thread or just describe my problem here (if anyone is interested in helping me)?
P.S.
What I do is (I'm on a PC) push the “PrtScr” (short for “Print Screen”) button along the top of my keyboard, which takes a screenshot of the screen and puts it in the clipboard, and then Ctrl+V (paste) into my photo editor (I use Paint.net), then crop out anything I don't want, upload the cropped image to photobucket, then insert the image with the little tree button along the top of the forum post thingy. (Could you tell me how you can insert images of the code into the messages ? I cannot do that.)
Edit: Wording
Edit 2: Punctuation
Last edited by zacadoole1 (May 11, 2013 13:31:59)
- andre_rifaut
-
100+ posts
I need to separate words in an answer
Many thanks for the info about photobucket. I did not know about it.
Text-based-games are not so easy to create. Do you think you could join your code to some MineCraft game so that to obtain a Minecraft game with a very rich set of commands ?
Text-based-games are not so easy to create. Do you think you could join your code to some MineCraft game so that to obtain a Minecraft game with a very rich set of commands ?
- andre_rifaut
-
100+ posts
I need to separate words in an answer
Did you notice that you gegin with letter 0 instead of 1 because you set Letter counter to 0 and then you use letter “letter counter” of answer.
This is why you have to “repeat length of answer + 1”
Usually I prefer to increment the index/counter at the start of the repeat to be sure I do not forget it and to be clear with its value that will be used in the rest of the repeat.
I your case you should at least to initialise letter counter to 1 instead of 0
This is why you have to “repeat length of answer + 1”
Usually I prefer to increment the index/counter at the start of the repeat to be sure I do not forget it and to be clear with its value that will be used in the rest of the repeat.
I your case you should at least to initialise letter counter to 1 instead of 0
- zacadoole1
-
100+ posts
I need to separate words in an answer
I'm pretty dedicated right to making this entirely text game, I've been working on it for a Text-based-games are not so easy to create. Do you think you could join your code to some MineCraft game so that to obtain a Minecraft game with a very rich set of commands ?long time now and it'd be a big accomplishment for me to get it finished.
Wow, thanks, I was wondering why I had to do ((length of (answer) + 1), I'll be sure to give you a special thanks in the notes and credits section of the project. Did you notice that you gegin with letter 0 instead of 1 because you set Letter counter to 0 and then you use letter “letter counter” of answer.
This is why you have to “repeat length of answer + 1”

- Creeper179
-
32 posts
I need to separate words in an answer
What is the block that says:
change by 1?
change by 1?
- drmcw
-
1000+ posts
I need to separate words in an answer
What is the block that says:
change by 1?
Wow you've resurrected a thread from just about a year ago!
will appear when you create a variable.
- Discussion Forums
- » Help with Scripts
-
» I need to separate words in an answer