Discuss Scratch

grumpyben
New Scratcher
2 posts

Lost parent looking for help with a letter replacement script

my son is working on a school project for his 1st year in computing in high school and he has to work on a script that asks him if someone enters a word if that word contains the letters A to M then it changes to a symbol, of his choice.
he's been asking me for ideas and I'm clueless when it comes to scratch etc any advice or examples I can see that I can then try to nudge him in the right direction?
many thanks a confused parent

Last edited by grumpyben (July 9, 2023 20:03:18)

Th5t0nekn0wnScratch
Scratcher
13 posts

Lost parent looking for help with a letter replacement script

Hi, could you try and explain a little further? Like exactly what you want the code to do?
grumpyben
New Scratcher
2 posts

Lost parent looking for help with a letter replacement script

He explained it to us like so

he has to make s program in scratch that when a user enters a word like “callum” it takes that input and will look at the word and if there are any letters that fall between a,b,c,d,e,f,g,h,I,j,k,l,m, it changes them to a symbol he chooses so the output should be “££££u£” if the input was callum that is and it should show up on his screen as the answer.

i might have missed some stuff so apologise for that



Th5t0nekn0wnScratch wrote:

Hi, could you try and explain a little further? Like exactly what you want the code to do?
THEPiG22
Scratcher
29 posts

Lost parent looking for help with a letter replacement script

Hi, this script should help

You do have to make a list (under the variable section above the custom blocks) that has the letters A to M in it like in the second screen shot tho, if you need any other help the forums are always here!



scratchcode1_2_3
Scratcher
1000+ posts

Lost parent looking for help with a letter replacement script

You could save each letter into a list and individually check each list item with a counter value, then use a loop to replace every item with another list that contains the letters A-M and checking if any of those items from the A-M list are in the input list, which get replaced by the symbol of his choice. Here is a basic script of that with the scratchblocks plugin:
when green flag clicked
ask [Type in a word] and wait
set [inputWord v] to (answer)
set [i v] to [0]
repeat (length of (inputWord))
change [i v] by (1)
add (letter (i) of (inputWord)) to [willChange v]

end
set [i v] to [0] // we have to reset i back to 0 to make another loop that actually replaces the items and adds all that into the final variable
set [finalValue v] to [] // this is not 0, or even a space. make this completely empty
repeat (length of [willChange v] :: list)
change [i v] by (1)
if <[letters A-M v] contains (item (i) of [willChange v]) ?> then // the letters A-M list should have all the letters A-M in a separate item
replace item (i) of [willChange v] with [$] // can be any symbol, even though i'm not sure about % since it is usually the symbol to blame for a few glitches
end
set [finalValue v] to (join (finalValue) (item (i) of [willChange v])) // this goes outside of the if-then so it always adds to the final value even if it's not A-M
end
say (finalValue) for (2) secs // this can be displayed using any other method too


Note: I haven't tested if this works yet, just made it off the top of my head, but I will come back after testing!



Hope this helps!

Last edited by scratchcode1_2_3 (July 9, 2023 20:55:08)

scratchcode1_2_3
Scratcher
1000+ posts

Lost parent looking for help with a letter replacement script

THEPiG22 wrote:

(#4)
Hi, this script should help

You do have to make a list (under the variable section above the custom blocks) that has the letters A to M in it like in the second screen shot tho, if you need any other help the forums are always here!


-snip-
NOO I GoT NINJA'D

wait I don't think that's what the person meant, it's not literally changing the costume to “a symbol” but replacing every letter that is within A-M TO a symbol XD
scratchcode1_2_3
Scratcher
1000+ posts

Lost parent looking for help with a letter replacement script

Good News: the code works!
This is a screenshot of the 3.0 code used in the actual project I made, using the same one I posted first:

Then, here is the result of the script, with me typing words such as callum (the same one you used), hello, yoshi, and backpack:

Now, here is the code but with some wait blocks, or delays to show the process happening in real-time:

And here is the delayed version of the code running:



Hope this helps!
THEPiG22
Scratcher
29 posts

Lost parent looking for help with a letter replacement script

scratchcode1_2_3 wrote:

THEPiG22 wrote:

(#4)
Hi, this script should help

You do have to make a list (under the variable section above the custom blocks) that has the letters A to M in it like in the second screen shot tho, if you need any other help the forums are always here!


-snip-
NOO I GoT NINJA'D

wait I don't think that's what the person meant, it's not literally changing the costume to “a symbol” but replacing every letter that is within A-M TO a symbol XD

Oh lol that makes more sense
Spentinium
Scratcher
100+ posts

Lost parent looking for help with a letter replacement script

I think this code will also work. It's smaller and doesn't use lists:
define replace characters: (chars) of string: (str) with: (replacement)
set [output v] to []
repeat (length of (str))
if <(chars) contains (letter ((length of (output)) + (1)) of (str))? :: operators> then
set [output v] to (join (output) (replacement))
else
set [output v] to (join (output) (letter ((length of (output)) + (1)) of (str)))
end
end

I ran a speed test comparing my code with their code and mine is nearly 2x faster ☠️☠️☠️
https://scratch.mit.edu/projects/873163543/

Last edited by Spentinium (July 10, 2023 00:51:28)

Powered by DjangoBB