alice-teacher Alice 2 what as a string

Robert Durtschi robert.durtschi at gcsu.edu
Tue May 13 17:51:36 EDT 2014


Observation:

If you want to do some math within "what as a string" you will need to do it outside of the function then drag the equation into and replace the "what" part.  The down arrow next to the expression being converted only allows it to be replaced by another expressing.

Example: displaying the total time taken to complete a game (for instance) rounded to the nearest tenth of a second:
variables: 
    startTime - set to world function timeElapsed with a "when the world starts" event

   time - set to timeElapsed - startTime when you reach your goal.

critter.say "it took you " joinedWith (time as-a-string) joinedWith " seconds" 

can produce something like: "it took you 5.5600002 seconds"

Common technique for rounding to the nearest tenth is to multiply by 10, add 0.5, take the integer, then divide by 10:

    time = int ( (time*10) + 0.5) / 10

or if the language provides a "round" function, which Alice does:
    time = round (time*10) / 10 

However what-as-a-string does not allow math in the drop-down arrow, most likely because it is a string function, or because it was overlooked.

the easiest way, and, in my opinion, the clearest, most explicit way to accomplish the rounding is to just perform the math in a separate statement just before displaying/saying it.

    time = round (time*10) / 10 
    critter.say "it took you " joinedWith (time as-a-string) joinedWith " seconds" 

However, if you consider it more elegant to have the rounding process as part of the "say" method you can just drag the right-hand part of the rounding statement over the "time" part of the "say" statement.

Alice has the side-effect of moving something dragged from within the edit window, rather than copying it (or at least moving  part of it), but since you don't need the first statement any more you can just delete the remains of it.

Bob Durtschi


More information about the alice-teachers mailing list