This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-08-20 11:56:31

ashfm
New Scratcher
Registered: 2011-08-17
Posts: 16

String into Integer

I am struggling with data types in Scratch. Is there a good section on this topic. Can a string be coverted into an integer or float?

Thanks

Offline

 

#2 2011-08-20 12:16:20

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: String into Integer

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!


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#3 2011-08-20 12:22:33

ashfm
New Scratcher
Registered: 2011-08-17
Posts: 16

Re: String into Integer

Thanks Harakou

Offline

 

#4 2011-08-20 21:40:51

scmb1
Scratch Team
Registered: 2009-03-19
Posts: 1000+

Re: String into Integer

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.

Last edited by scmb1 (2011-08-20 22:04:41)


http://i48.tinypic.com/2z5pqad.png

Offline

 

#5 2011-08-20 22:35:53

4lover
Scratcher
Registered: 2010-04-24
Posts: 500+

Re: String into Integer

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


Check out this awesome new zombie-cod type of game: http://scratch.mit.edu/projects/4lover/1975649

Offline

 

Board footer