Discuss Scratch

Quantix
Scratcher
30 posts

Detecing if a variable is a number or a word

Hello. I need some help…

I need to detect if a variable contains just numbers or if it has letters.
I tried this:
if <((text) - (1)) = [NaN]> then
script
end
but it didn't work. help please?

Last edited by Quantix (Oct. 8, 2014 18:49:48)

TheLogFather
Scratcher
1000+ posts

Detecing if a variable is a number or a word

I'm guessing you want to test an answer that has been typed in? If so, try something like this:

set [test v] to (answer)
change [test v] by (1) // this gives "NaN' if the answer wasn't a valid number
if < (test) = [NaN] > then
not a number
else
is a number
end

If you actually want to check that the answer contains only digits (rather than any valid floating point format number, like “-34.567E-89”), then you might find the “String manipulation” project in my sig useful.

Quantix
Scratcher
30 posts

Detecing if a variable is a number or a word

Thanks. That's what I was looking for but my version didn't work.
DadOfMrLog
Scratcher
1000+ posts

Detecing if a variable is a number or a word

Oh, oops - sorry, wrong account!

Try the one in *this* sig…

EDIT:

Quantix wrote:

Thanks. That's what I was looking for but my version didn't work.
yeah, mathematical operators end up taking a non-numerical variable's value as zero, which is a bit inconsistent

Last edited by DadOfMrLog (Oct. 8, 2014 19:17:48)

Harakou
Scratcher
1000+ posts

Detecing if a variable is a number or a word

DadOfMrLog wrote:

Oh, oops - sorry, wrong account!

Try the one in *this* sig…

EDIT:

Quantix wrote:

Thanks. That's what I was looking for but my version didn't work.
yeah, mathematical operators end up taking a non-numerical variable's value as zero, which is a bit inconsistent
Yeah, it is a bit odd. Another thing you can do is something like this:
if <<not <((1) / (var)) = [Infinity]>> or < [var] = [0]> > then
var is a number
end

Which should be a one-liner way of doing it without extra variables or changing its value.

Last edited by Harakou (Oct. 8, 2014 19:47:22)

TheLogFather
Scratcher
1000+ posts

Detecing if a variable is a number or a word

Harakou wrote:

if <<not <((1) / (var)) = [Infinity]>> or < (var) = [0]> > then
var is a number
end
Ah yes - nice idea.

How about:
if < ( ( (var) / (var) ) = [1] ) or < (var) = (0) > > then
var is a number
end
Note that, unlike Harakou's test, the one just above claims “Infinity” and “-Infinity” are not numbers, which may or may not fit with what you actually want to happen…


Or, equivalent to Harakou's (but a bit less verbose):
if < ( ( [abs v] of (var) ) > [0] ) or < (var) = (0) > > then
var is a number
end

Last edited by TheLogFather (Oct. 8, 2014 20:31:53)

Diamond_Scratcher
Scratcher
26 posts

Detecing if a variable is a number or a word

if <((var) / (1)) = (var)> then
Done.
end

Last edited by Diamond_Scratcher (March 25, 2015 02:31:51)

Diamond_Scratcher
Scratcher
26 posts

Detecing if a variable is a number or a word

if <(round (var)) = (var)> then
Done.
end

Last edited by Diamond_Scratcher (March 25, 2015 02:34:13)

Airhogdog
Scratcher
37 posts

Detecing if a variable is a number or a word

i think variables are only numbers in real coding and typing and stuff, but I dont know.
Arthurtilly
Scratcher
1000+ posts

Detecing if a variable is a number or a word

Variables can be integers, strings, floats and the other things in any type of coding.
Diamond_Scratcher
Scratcher
26 posts

Detecing if a variable is a number or a word

laserarrow6
Scratcher
11 posts

Detecing if a variable is a number or a word

Diamond_Scratcher wrote:

if <(round (var)) = (var)> then
Done.
end

This will additionally confirm that the variable is an integer, correct?
asivi
Scratcher
1000+ posts

Detecing if a variable is a number or a word

laserarrow6 wrote:

Diamond_Scratcher wrote:

if <(round (var)) = (var)> then
Done.
end

This will additionally confirm that the variable is an integer, correct?

This topic is not about integers…
Please do not revive old resolved topics. Thanks.
neeb132
Scratcher
500+ posts

Detecing if a variable is a number or a word

if <<(answer)=[0]>or<not<((answer)*(answer))=[0]>>> then
...
end
motherofhiers
Scratcher
1 post

Detecing if a variable is a number or a word

TheLogFather wrote:

I'm guessing you want to test an answer that has been typed in? If so, try something like this:

set [test v] to (answer)
change [test v] by (1) // this gives "NaN' if the answer wasn't a valid number
if < (test) = [NaN] > then
not a number
else
is a number
end

If you actually want to check that the answer contains only digits (rather than any valid floating point format number, like “-34.567E-89”), then you might find the “String manipulation” project in my sig useful.


How can I make this work for letters?
Code must search through string, if it finds anything that is not a letter it prints an error message.
Reign_Sky
Scratcher
100+ posts

Detecing if a variable is a number or a word

motherofhiers wrote:

TheLogFather wrote:

I'm guessing you want to test an answer that has been typed in? If so, try something like this:

set [test v] to (answer)
change [test v] by (1) // this gives "NaN' if the answer wasn't a valid number
if < (test) = [NaN] > then
not a number
else
is a number
end

If you actually want to check that the answer contains only digits (rather than any valid floating point format number, like “-34.567E-89”), then you might find the “String manipulation” project in my sig useful.


How can I make this work for letters?
Code must search through string, if it finds anything that is not a letter it prints an error message.

Oh, I've been doing this for my question scrips for a while. You check it using the mod script, and set definable boundaries for your number. Here's a example

when I receive [Question v]
set [counter v] to [1]
set [number v] to []
set [letter v] to []
ask [Letter or Number?] and wait
repeat (length of (answer))
if <<((letter (counter) of (answer)) mod (1)) = [0]> and <<(letter (counter) of (answer)) < [10]> and <(letter (counter) of (answer)) > [-1]>>> then
set [number v] to (join (number) (letter (counter) of (answer)))
else
set [letter v] to (join (letter) (letter (counter) of (answer)))
end
change [counter v] by (1)
end
if <(length of (number)) > [0]> then
say [Invalid Answer...] for (2) secs
broadcast [Question v]
end

Btw, when I made the variable number, I made it that way so you could tell you were getting the numbers. You can have it throw an error message by making it say error and stopping the script. Also, let me tell you why this works. The variable counter checks every letter of the answer, and the mod script checks if the number is divisible by one. So by using mod, I am checking if it is a number, and I'm setting the limit between -1 and 10 so characters are not mistakenly registered as numbers. If neither condition goes through, it means that this is not a number for sure, and so it goes into the else script, where you can work your magic.

As I'm seeing this is cut off, I'm going to post a link to a project detailing this. https://scratch.mit.edu/projects/205908801/#player

Last edited by Reign_Sky (Feb. 23, 2018 21:50:52)

ParadoxScratching
Scratcher
100+ posts

Detecing if a variable is a number or a word

So far as I know, Scratch 3.0 will contain a feature “x contains y” which would make this simpler.
crazyweasel675
Scratcher
44 posts

Detecing if a variable is a number or a word

if <(((input) + (1)) - (1)) = (input)> then
code to do if input is a number
else
code to do if input is not a number
end
Akalanka123
New Scratcher
1 post

Detecing if a variable is a number or a word

Add a conditional statement that checks whether the answer is a number that is outside the range of 1 and 10.
If the answer was not in the range, the Sprite should say “You did not enter a number in the range 1 to 10”
anyone know how to do this?
codeman1044
Scratcher
1000+ posts

Detecing if a variable is a number or a word

Welcome to scratch! Please check the date before posting anything on a thread. This is called necroposting, which brings back old and answered topics to the front page, meaning that a thread that someone needs answered might get hidden or moved farther back.

Last edited by codeman1044 (April 20, 2019 01:34:35)

Powered by DjangoBB