Alice Community

Alice Community (
-   How do I...? (
-   -   Ask user for a 2-digit Number? (

JustasM 02-23-2009 10:49 AM

Ask user for a 2-digit Number?
1 Attachment(s)
Attempting to answer jaydenb's question on asking the user for an age, I stumbled across a problem. To ask for a user's age, one should supposedly be able to make a number variable in the world, then, under my first method, set the value to (ask user for a number)[function]. However, apparently, the user can only enter single digit numbers. If you enter a double digit number, the number variable is set to 0.

Why is this? Is a two-digit number not considered a number? And finally, is there a way to ask the user to input a two digit number?

I have attached the world I was working on, to make it more clear. If you press Play, and enter a two digit-number when it asks for your age, it gives the age as 0. Further down, I experimented with using separate variables for each digit, but that is very cumbersome, and doesn't work if you want to work with the entire age value.

DrJim 02-23-2009 12:24 PM

1 Attachment(s)
Very strange! :rolleyes:

The attached file is a slight modification of the file I posted in response to the original question and seems to work fine. I even added the Eskimo object to make sure there wasn't something strange with it.

The main difference between your posting and the attached is that both the name and age are local variables to MyFirstMethod instead of being defined as world level properties. Local variables are reset to their default value when the method ends while this doesn't occur for world level variables until the world stops.

However, since both events occur at the same time in your program, this shouldn't make a difference. Yet it does for the number - but [B]not[/B] the string. :o

It's probably better to use local variables here anyway - since in general that's better practice - so this is a solution and actually a pretty good one. But why the problem occurs is a mystery to me.

JustasM 02-23-2009 01:29 PM

Thank you for the reply...

However, I still encounter the same problem, even with the world you posted. Might this be some problem with my version of Alice? (I'm using v2.2 9/1/2008) :confused: I've never experienced any major problems with it before. On the other hand, I've never really used strings or have been asking the user anything in my worlds.

The tip about it being better practice to use local variables is also very useful.. I am largely self-taught in Alice, and have not used any book, so my coding is probably not top notch. I've looked a bit at Dick Baldwin's guide, but I thought that I had already nailed most of the basic stuff down...

Thank you all the same. ;)

~~EDIT~~ I have just tried opening your world in Alice 2.0, and it worked smoothly (unlike the application- I'm using Windows Vista). Thus, it must be a problem with v2.2... Maybe I should report it?

DrJim 02-23-2009 05:51 PM

1 Attachment(s)
Actually in experimenting a bit more, I found out that my first guess as to the problem was incorrect. (Not unusual, unfortunately. :()

If you try to run the attached in ver 2.0 (using Vista), I think you'll find it works OK - at least until you get to the display of the single digits which I have no idea as to how to fix. I really don't think there is a clean way to display a number without the decimal point.

Your first problem seemed to be related to a known bug - namely that when you use the "ask user for a string", the question sometimes repeats. I don't remember the reason but I believe the probem is documented somewhere.

Adding the first wait statement seemed to clear up everything.

Re 2.0 vs. 2.2 - I had the same problem initially in 2.0 so I doubt it's just a version problem. Personally, I don't use 2.2 (it uses too much memory for the improvements you get) so I really can't say for sure.

Re using local variables - Since that relates to encapsulation, which is poorly (if at all) supported in Alice 2.0, I suspect that is the reason Dick didn't include the subject in his tutorials. He has done some good postings in this forum and other places on the subject - I suggest you do a Google search on "Baldwin" and "encapsulation" if you're really interested.

All times are GMT -5. The time now is 09:25 AM.

Copyright ©2021, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.