Discuss Scratch

Scratch-Animator-371
Scratcher
6 posts

Checking if answer contains certain characters.

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?
awesome-llama
Scratcher
1000+ posts

Checking if answer contains certain characters.

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.

Last edited by awesome-llama (March 24, 2024 15:11:00)



Scratch-Animator-371
Scratcher
6 posts

Checking if answer contains certain characters.

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
Scratcher
1000+ posts

Checking if answer contains certain characters.

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

Last edited by LP372 (March 26, 2024 09:50:23)


Projects I want people to see:
https://scratch.mit.edu/projects/1018171720

Powered by DjangoBB