Checking if answer contains certain characters.

id: 749888

category: Help with Scripts

posts: 4

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?
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.

awesome-llama wrote:

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.

Yeah, but it doesn't report correctly if the answer contains both letters and numbers.
when green flag clicked
forever
say [help!!!!]
end
LP372 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