Checking if answer contains certain characters.
id: 749888
category: Help with Scripts
posts: 4
Mar 24, 2024 14:57:20
Checking if answer contains certain characters.
view on scratch
view on legacy ocular
Scratch-Animator-371
loading
Hey, I am working on a project in which the sprite asks the user to input something and it checks whether it contains letters, as it should only have numbers. I know we can use the
<<> or <>>block but it would take a lot of space, any ideas?
Mar 24, 2024 15:08:58
Checking if answer contains certain characters.
view on scratch
view on legacy ocular
awesome-llama
loading
It's easier to check if the input is not a number.
You can check if it is a number with something like this:
The addition block converts the variable into a number and compares it with what it was before. Numbers remain the same because 0 was added. Strings however are converted to the number 0 which is no longer the original string and returns false. Note that the string “0” as well as any other string representing a number will be seen as a number, but this is fine and intended with how Scratch handles data types.
You can check if it is a number with something like this:
<((var) + (0)) = (var)> // true if var is a number
The addition block converts the variable into a number and compares it with what it was before. Numbers remain the same because 0 was added. Strings however are converted to the number 0 which is no longer the original string and returns false. Note that the string “0” as well as any other string representing a number will be seen as a number, but this is fine and intended with how Scratch handles data types.
Mar 26, 2024 02:26:57
Checking if answer contains certain characters.
view on scratch
view on legacy ocular
Scratch-Animator-371
loading
Yeah, but it doesn't report correctly if the answer contains both letters and numbers. It's easier to check if the input is not a number.
You can check if it is a number with something like this:<((var) + (0)) = (var)> // true if var is a number
The addition block converts the variable into a number and compares it with what it was before. Numbers remain the same because 0 was added. Strings however are converted to the number 0 which is no longer the original string and returns false. Note that the string “0” as well as any other string representing a number will be seen as a number, but this is fine and intended with how Scratch handles data types.
when green flag clicked
forever
say [help!!!!]
end
Mar 26, 2024 09:45:46
Checking if answer contains certain characters.
view on scratch
view on legacy ocular
LP372
loading
I'll create a project for it, you just need to use the code i use and implement it
I'll put it in once it is done
https://scratch.mit.edu/projects/989808240
tell me if i made a mistake
I'll put it in once it is done
https://scratch.mit.edu/projects/989808240
tell me if i made a mistake