PDA

View Full Version : make people say variable values


sidny4
09-23-2006, 02:34 PM
I have a variable num which is initialized to 0. I have 3 people, one is going to give 4 numbers, one is going to add those four numbers up, an one is going to give the average of those four numbers. Is there a way to have one of the people say the value of my variable num?

lanceA
09-23-2006, 02:48 PM
Try making your variable to be of type STRING and store your number in it as a string.

Then when you add object.say to your program and it asks WHAT select expressions and select your variable's name. When the program is run your object will "say" the number.

This may help.

[Edit] - That may not help. I just tried using the "absolute value" function on a String, hoping to have it return and numeric, and it doesn't appear to work on strings.

Sorry

[Edit] Last Edit: You could create two variables, one of type Number and one of type String. Store your number in both of them and use one to SAY and one to be used in any mathmatical functions.

Good luck

DrJim
09-23-2006, 07:45 PM
Think this is true but just spent ten minutes trying to find it in print with no luck (LanceA - I'm going to have to start your notebook method).

I don't think you would really need two variables - with all the mess that would cause keeping things straight - just one numeric one which then you would convert to a string each time to say it.

I think in Alice you can always convert from a number to a string but not the other way around - will post again when I have verified or disproven this.

Jim

lanceA
09-23-2006, 08:33 PM
GREAT! Please tell me how to "convert" a String to a numeric in Alice.

I can't use the java Integer. parseInt() method.

DrJim
09-24-2006, 12:57 PM
Pretty sure string -> number conversion can't be done using Alice. The only two string functions under World are concatenation and "--- as a string," not even a "find in string" or "extract from string" type functions. I can't find any elsewhere in the program.

The "--- as a string" function works fine with numbers, which was the original question.

I haven't found a way to execute any Java or other programs from within Alice - I would like to know if there is a way (particularly for Jython, since there is a lot of Python code for Blender).

Even if there is, it's going to be a bit messy to set up this conversion since, at least according to the original documentation (thesis and proposal), Java only supports floating point and that with one format (123.45).

Jim

(P.S. Did find the source of the "set first class to FALSE" technique - it was an old posting here by Randy Pausch. It also says there are no other effects from doing this.)

MiR
11-19-2006, 12:39 PM
Look at
http://www.alice.org/bvw02fall/scripting/scripting.html
and
http://www.westga.edu/~drocco/alice/scripting_basics.html
for Jython scripting with Alice.

rtb451
12-20-2007, 01:15 PM
You can make objects say integers by:
1. Add say method
2. Click on the world tile in the object tree
3. Click on functions tab
4. Scroll through function, find "what as a string" function
5. Drag "what as a string" and drop it into object say function in place of
string

DickBaldwin
12-20-2007, 03:15 PM
I have a variable num which is initialized to 0. I have 3 people, one is going to give 4 numbers, one is going to add those four numbers up, an one is going to give the average of those four numbers. Is there a way to have one of the people say the value of my variable num?

Yes there is.

See http://www.dickbaldwin.com/tocalice.htm You will probably need to use the String concatenation function belonging to the world. Therefore, a shortcut might be to go to Google and search for the following keywords:

baldwin alice string concatenation

Then go to the very last Google page and click on the link that reads:

"repeat the search with the omitted results included"

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

gmcquade
05-21-2010, 10:25 AM
I am starting to teach Alice 2.2 to my grade 10 and 11 students. I created a pgm where a cowboy shots at a bottle and the pgm is to keep track of the number of misses. At the end, the cowboy would say "I missed 3" for example. This is a concatentation of "I missed" + cowboy.completeMiss (numerical variable I created). The statement to combine the two data types and to have the cowboy say it is as follow:

cowboy say ( I missed joined with ( cowboy.completeMiss as a string ) )

It used two functions:
1) a joined with b World string function
2) a as a string World string function

haven812
11-30-2010, 06:25 PM
here is a function you can put in the world script, using the script function on alice, to turn a string into a number...


def convertString(s):
"""Convert string to either int or float."""
try:
ret = int(s)
except ValueError:
#Try float.
ret = float(s)
return ret


... after putting this into the world.script, you can call it by placing convertString("the string you want to change to a number")
wherever you want to output the number. An example script could be

def convertString(s):
"""Convert string to either int or float."""
try:
ret = int(s)
except ValueError:
#Try float.
ret = float(s)
return ret
number = "73"
world.value=convertString(number)



This works with decimals and negatives as well, but an error will occur if the string is something like "a" or "a124143", it has to be all numbers.