Discuss Scratch

Mockingjade
Scratcher
100+ posts

Is there a way to detect if an input is a number or a text?

Is there a way to detect if an input is a number or a text?

For example,
ask [Number from 1 to 10?] and wait

if <(answer) = [number]> then
do something here
end

I need this for a project I'm working on. Any help would be greatly appreciated. Thank you!

~Jade
TheCodeMan2000Test
Scratcher
3 posts

Is there a way to detect if an input is a number or a text?

You could make a list with the numbers 1-10 then do this:

if <[1 through 10 v] contains (answer) thing ?> then
do something here
end
mstone326
Scratcher
1000+ posts

Is there a way to detect if an input is a number or a text?

You can divide the 2 / answer.

If it comes up infinity it was not a number. Or else it is a number

https://scratch.mit.edu/projects/384408224/
PizzaAddict4Life
Scratcher
1000+ posts

Is there a way to detect if an input is a number or a text?

if <<(letter (1) of (answer) = [0] or <(Just do that same thing again for numbers 1-10.)>> then
Do stuff
end

If it is a number, it will detect it. If it is a letter, it assumes the rest is a word

Last edited by PizzaAddict4Life (April 10, 2020 17:51:08)

LordThror
Scratcher
100+ posts

Is there a way to detect if an input is a number or a text?

You could detect if the variable is greater then 0. I am assuming that if it is text it would come back as false.
Mockingjade
Scratcher
100+ posts

Is there a way to detect if an input is a number or a text?

Thanks for the tips! I'll try them out.
Scratch-Minion
Scratcher
1000+ posts

Is there a way to detect if an input is a number or a text?

If you are testing only for the numbers 1 to 10 (ie. no decimals, not 0, not negative)

if <<(answer) > [0]> and <<(answer) < [11]> and <<((answer) - ([floor v] of (answer))) = [0]> and <((answer) * (1)) = (answer)>>>> then

end

The first two conditions check the number is in the range 1-10
answer - floor of(answer) = 0 checks that there are no decimal bits eg 3.5
answer * 1 = answer checks that it is a number eg. 3A will pass the third test but not this one

If you have a range that includes 0 then you should add one more test “if answer = 0” as 0A would pass all the above tests.
bacphu123
Scratcher
100+ posts

Is there a way to detect if an input is a number or a text?

set [variable v] to (answer)
change [variable v] by (1)
if <[variable] = [NaN]> then
do something here
end

if you change the variable by one and if its a letter the variable will be NaN
Oumuamua
Scratcher
1000+ posts

Is there a way to detect if an input is a number or a text?

bacphu123 wrote:

set [variable v] to (answer)
change [variable v] by (1)
if <[variable] = [NaN]> then
do something here
end

if you change the variable by one and if its a letter the variable will be NaN

nope
Oumuamua
Scratcher
1000+ posts

Is there a way to detect if an input is a number or a text?

Last edited by Oumuamua (April 11, 2020 09:00:00)

PizzaAddict4Life
Scratcher
1000+ posts

Is there a way to detect if an input is a number or a text?

LordThror wrote:

You could detect if the variable is greater then 0. I am assuming that if it is text it would come back as false.
No, it comes back as true

Powered by DjangoBB