I'm writing a program (for an assignment) and needed help identifying if an item on a list contains the letter "e", for the program to say it. The program would then sort through the list, dictating only the ones with it.
Another script I need help with is one where the program checks for any item that is not the first or last two. It's supposed to say the name of the item if it's not the first or last two on the list.
For this one I need to automate the process, it should work no matter how many items are on the list. It seems like it should work in a way, but no.
This one obviously doesn't work. Put Nate and Claire together and you get NaCl, basically how I feel (salty).
Think you guys can help a fellow goober out? A little hint might be useful...
< _ contains _ > a string, but you need to use a list instead.
when [ 5 ] key pressed
set [ list ] to ( list [ John ] [ Robert ] [ Joe ] [ Claire ] [ Nate ] [ Billy ] )
script variables ( index )
set [ index ] to 1
repeat ( length ( list ) )
if < ( item ( index ) of ( list ) ) contains [ e ] >
say ( join [ Welcome, ] ( item ( index ) of ( list ) ) [ . ] ) for ( 2 ) secs
change [ index ] by ( 1 )
The underlined part (
^^^^) is where the problem is.
Right click a script and press
script pic... to take a photo of a specific script or block.
Snap! isn't English. In English you can say "if he's either John or Fred..." but in Snap! (or any programming language) = (well in some peculiar languages ==) is a function that takes two inputs and reports TRUE if they have the same value. A call to = does exactly one comparison. (To everyone else: yes I know, don't confuse them.) You are trying to do four comparisons.
Another way to think about this is to note that the inputs to OR are supposed to be TRUE or FALSE values. But you're giving it, e.g., ITEM 1 OF (LIST), which is a word, not a Boolean (TRUE/FALSE) value.
So write an expression that does four separate comparisons and combines the results.
But you can do better than that. You want to know whether the item is at index 1, 2, length-1, or length. So there's no need to look at the values at all! Just see if INDEX is one of those four numbers.
Also, Snap! provides looping constructs, so you don't have to say CHANGE INDEX BY 1 explicitly. You can say
I thought about suggesting FOR EACH ITEM, but they need to manipulate the positions of items in the list, so they'd have to use the library version with the # upvar, and even then it'd be awkward.
If you're going to do that, instead of using KEEP you could just give ITEM a list as inputs.
Assuming you mean:
That will work, but only if the length of the list is 5 or more, so it will require an extra test, whereas the KEEP solution is always valid.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.