Please help me with a special-character checker

I’m currently taking AP CSCI P and my teacher uses Snap!. I’ve been fooling around with the Editor and I’m struggling badly to make a program, that’s part of an account sign-up/login system, where the user will input a user, and it will be checked to make sure it doesn’t have any forbidden characters in it (ie,!"#$%…).

Here is what I have so far:

I tried to use an or instead of doing each individual character, but I couldn’t get that to work either.

I managed to get a login-system to work, but only with a preset user and pass:

Please tell me what I’m doing wrong (:pleading_face:), and how I could possibly optimize this so it’s not like 40 ifelses.

How far along are you in the curriculum? Are you at the beginning or the end of your school year? More specifically, have you learned about the


block? You could (only once!) make a list of all the punctuation characters and then either look at each item of that list, or I’d be more inclined to do it the other way, FOR EACH the letters in untitled script pic (8) and see if each of the letters in ANSWER is contained in the list of punctuation. It’s not that your way is wrong, just kind of unusual.

An even neater way to do it uses higher order functions to do all the checking in parallel.
untitled script pic (10)
Would you use MAP, KEEP, or COMBINE? The result you want is a list of the punctuation characters in ANSWER.

A couple of tips that might help.

  1. Removing the if/elses. You can actually click on the arrowhead in the regular if block to create else if clauses. This avoids the extreme nesting you were doing.
  2. Catch/throw. You don’t need to use catch and throw here because the else clauses won’t run if the if clause was true. If your code shown isn’t complete, that’s a different story, but I don’t see why you need it here.
  3. WARP. It is true that warping can help speed certain processes up, but in your case I think it more likely that it won’t help. WARP stops the script from “yielding”. Yielding occurs at the end of a loop and is where the script pauses running to allow another script (thread) to run (Snap never truly runs two things at once). The key here is that you aren’t dealing with a loop, so Snap won’t be yielding to other scripts in the middle and you won’t be getting much in speed savings.
More details on WARP

Snap will actually jam as many “iterations” of a loop into one frame as possible as long as that script doesn’t update the stage (drawing, movement, costumes). This means that warp is generally only useful when you’re doing something on the stage (i.e. drawing) and you want Snap to run all the iterations of your loop in one go.

  1. Removing the if/elses altogether. This is what @bh (who beat me to it in a reply) was talking about.
Don't read this without knowing your answer

Technically faster would be FIND FIRST, if all you care about is knowing that “hey, there’s an invalid character”, because it stops the moment it finds one invalid entry. Something like

Also, make sure to turn on USE CASE SENSITIVITY or else it will be case insensitive.

untitled script pic (47)
This returns true if the string in the first inputs has any letters in the second.

Thank you. I had no clue that if was expandable to elseif. I thought it just didn’t exist.

I did, thank you tho

So far I’m about 5 days into the curriculum, so the beginning.

We haven’t even learned how to comment yet, but I was fooling around with list and I didn’t really understand it. Everytime I tried to dupe it the data in it didn’t follow and I kept getting errors, so I decided it’d be best to not use it for right now.

I did think about trying to do everything at once by using a giant || statement with all the unicodes in it, but still no luck.

Also, I managed to sort of get it to work, though I couldn’t sort by character, so work-ish.

I have no idea if I would use MAP, KEEP, or COMBINE, however a little birdie has told me that I should probably use FIND FIRST…

I’ll give a quick guide to MAP, KEEP, COMBINE, FIND FIRST!

MAP applies a function to every item in a list. For example, MAP example would apply the function x+5 to every item in the list [1,2,3,4,5,6,7,8,9,10], meaning 5 gets added to each item, making [6,7,8,9,10,11,12,13,14,15].

KEEP keeps every item that fits the Boolean expression. For example, KEEP example would report [1,2,3,4], since the numbers 1-4 are all less than 5.

COMBINE combines all items in a list using a function. For example, COMBINE example adds together all of the numbers in the list to get 55, and COMBINE example 2 joins together all the numbers as a string to get “12345678910”.

Finally, FIND FIRST finds the first item in a list that matches a requirement. For example, FIND FIRST example would report 6, since 6 is the first item in the list that is greater than 5.

Feel free to add on or edit to this! (I made it a wiki)

I’m trying to use FFI but its only detecting the character if its alone by itself, not if its in the text (I tried splitting)

Could you send me a script pic?

I don’t have the trust level yet, but it looks like this:

ask ---- and wait
if (XOR(
(
find first item (
(split(answer)by(letter);) == (’ ‘);) in (list (//all special characters (in sep boxes));
) =/ (’ '))) {
alert(‘alert’);
};
alert(‘nothing’)
sorry if this is unreadable, my js syntax is rusty

So far I’m about 5 days into the curriculum, so the beginning.

Wow, and your teacher gave you this assignment? You or your teacher or both are brave.

Everytime I tried to dupe it the data in it didn’t follow and I kept getting errors

What do you mean by “dupe it”? Can you post an example? (If you haven’t learned this yet, you can right-click on a script, then choose “script pic” from the menu, and it’ll download a picture (.png) to your computer’s download folder. These are so-called “smart pics,” meaning that buried inside them is the actual code of the script, so if you drag one into Snap! the script will appear in your scripting area.)

Ahh, I see your problem! You are using the wrong function!
Let’s take a few steps back.
For the sake of simplicity, let’s say that answer is equal to “Hello!”.
Doing (split (answer) by [letter v]) reports a list like this:


When you run untitled script pic (64), you are checking if each character in the special characters list is equal to the list above, which is not what you want to happen. What you want is to detect if answer contains a special character, and to do that, you need a block that checks if a list contains a specific item.
Fear not! For there is a block just for that!
contains

The <@list contains []? checks if the desired item is in a list, just like in the example above!
Now, we can use that block instead of <[]=[]> to ensure that we check any special characters are in answer!
Got too lazy to make a name for this script pic
You may notice that the script above reports back the first special character, but it reports an empty string when there are no special characters in answer . We can use this behavior to check if a special character got reported back:

but it reports nothing

Don’t say that. It reports an empty word. Command blocks report nothing.

No, I’m just doing this for fun.

I mean duplicate the list. Whenever I try to duplicate a list with stuff in it, or move it, all that stuff dissapears

Thank you.
I thought that FFI would auto do the contained part.

Oops my bad. I guess I meant to say that it reports a value of nothing. Lemme fix that real quick…

Fixed!

No, it doesn’t report a value of nothing. It reports a value of an empty word. If you prefer, an empty text string.

I’m sorry, I’m being very teacherish. But it took the human race a thousand years or so to believe that 0 is an actual number, rather than the absence of a number. It’s the same with empty words. The empty word is a perfectly good word. It’s not nothing.

Whenever I try to duplicate a list with stuff in it, or move it, all that stuff disappears

I’m sorry, I’m still not understanding. You say
untitled script pic (7)
Then what? If you say, for example,
untitled script pic (8)
that’ll make BAR be a copy of FOO. (If you just say
untitled script pic (10)
that doesn’t make a copy; it just gives you another name for the same list.)

If you mean that you right-click a LIST block and choose “duplicate” from the menu, what you’re duplicating isn’t the list, but the block – a piece of a program.