Scratch is pretty ambiguous when it comes to variable types. It will accept any variable in any block that needs a reporter block and automatically interpret it as needed. For example, if you put a string into a block requiring a double, it will filter out the first sequence of numbers it can find and use that. If there are no numbers, it will just use 0 as a replacement.
You can also in effect convert a variable to an integer using the round( ) block.
Hope that helps!
Offline
What Harakou wrote is completely true. However, it is sometimes necessary to convert variable type because your program won't work if you do not. These instances aren't very common, and some people can make tons of complicated Scratch projects without running into them, but they are good to watch for.
For example, if you use the "ask" block to have the user enter a number, some of the operation blocks (the inequalities, if I remember correctly), won't function on the result. To fix this, you just have to turn the string that was entered into an integer or float by using some of the basic operation blocks on it. For example, you could simply multiply the value by one or add 0.
Another issue I've run into is accidentally putting non-integers into the "repeat ()" block. Say you have a variable "num" that you know is always an even integer because you programmed it that way. You make a script like this:
Unfortunately, Scratch doesn't know that "num" is even and thinks you are putting a non-integer in the block, and crazy things happen. The best solution here is to do as Harakou said and use the "round()" block to round the num/2 value.
Last edited by scmb1 (2011-08-20 22:04:41)
Offline
scmb1 wrote:
What Harakou wrote is completely true. However, it is sometimes necessary to convert variable type because your program won't work if you do not. These instances aren't very common, and some people can make tons of complicated Scratch projects without running into them, but they are good to watch for.
For example, if you use the "ask" block to have the user enter a number, some of the operation blocks (the inequalities, if I remember correctly), won't function on the result. To fix this, you just have to turn the string that was entered into an integer or float by using some of the basic operation blocks on it. For example, you could simply multiply the value by one or add 0.
Another issue I've run into is accidentally putting non-integers into the "repeat ()" block. Say you have a variable "num" that you know is always an even integer because you programmed it that way. You make a script like this:
http://oi56.tinypic.com/10hmwx1.jpg
Unfortunately, Scratch doesn't know that "num" is even and thinks you are putting a non-integer in the block, and crazy things happen. The best solution here is to do as Harakou said and use the "round()" block to round the num/2 value.
I agree with you 100 percent because that has happened to me once or twice
Offline