Quote:
Originally Posted by BadWolfGeronimo
This assignment is making me crazy. I feel pretty confident with loops but having the "Should I spin again?" question accept yes, Yes, and YES is something I cannot understand. My thought process is to use a variable with no as the value and set the boolean equation so that if the answer is not No, then it accepts it and spins again. But apparently booleans and myself do not get along. I have been working on this for days now. I've read all the other posts about lab 2 and I see someone made their own function to accept a b and c. How do I do that?
|
The way you have been trying would sort of work.
The problem is that, if done correctly, the program would accept anything except "No" as being yes. This would include "yes", "Yes", "YES", "no", "nO", and "Reading law in 2017 is boring." which is not what you want.
A couple of things before looking at a possible answer. Alice allows you to "play around" so in working out a problem you can (should, must!!) use this to understand how the program works. One of the most useful, and unmentioned, tools in this respect is the print instruction which you can access at the bottom of the developer window. Before using this make sure the movie is not running in full screen or you will not see the console window when the movie runs.
Also it is important that you know when a variable changes and when it resets. A variable that is created in the world get assigned a value when the world starts and remains the same unless something in the program changes it. When a variable is defined in a method or function it resets itself each time the method or function is called and will only change if something
in the method or function changes it.
It is also crucial to understand the "flow" of the program. As an example of this, lets look at a common error made by beginning students.
Code:
world.my first method
print "Starting Movie"
While true
print "doing while loop"
Wait 2 seconds
print "While loop done, moving on"
In this example, the last print statement will never be called and the movie will loop forever in the while statements. I included the Wait just so that the messages don't get generated faster than you can read them.
Side Comment:
The reason this runs at the start of the movie is because of the default When the world starts, do world.my first method event in the upper left portion of the development screen. If you created a new method, lets call it world.test, and replace world.my first method with world.test in the When the world starts event, world.my first method will never be executed UNLESS something in world.test or in a method called by world.test calls it.
This means you can create a test method like world.test and swap it in and out of the When the world starts event to switch between your testing/experimental/trial code and the movie you are actually creating.
Experiment with the information above, it will pay off.
Now that I have bestowed a headache on you, lets see if we can make it start throbbing.
Since this is your second lab, I am assuming you have not been introduced to creating method or functions so lets start by just using
world.my first method.
First of all start small and get the basics working. To that end lets assume that the user will only type in "yes" if they want to continue. Lets further assume that most of the time you want to continue, mostly because it makes the program easier.
In the
properties tab of the
world (meaning the world object is selected and you select properties in the tab on the right.), click on
create new variable - Give the variable a descriptive name, I would use something like fContinue.
- Select the Boolean type.
- Make sure the default is set to true.
- Click the "OK" button.
The f in fContinue reminds me that it is a Boolean (flag) variable and Continue reminds me exactly what it does.
In the world.my first method section add a variable, call it something like sAnswer. It can be set to the default string as its going to get changed before we use it.
The basic outline of your world.my first method will look like this:
Code:
world.my first method
sAnswer=default string
// Do the beginning stuff
print "Startup stuff done"
While world.fContinue
// Do the repeating stuff once each time we go through the loop
// Check if we are done
sAnswer set value to ask for user string question = Do you want to continue? (yes or no)
// Print the users answer
print "User answer: " joined with sAnswer
if sAnswer=="yes"
// We want to keep going so leave world.fContinue unchanged.
else
print "Exiting while loop..."
world.fContinue set value to false
// Do the shutdown stuff
print "Shutting down"
// Pause long enough to see the shutdown message.
Wait 2 seconds
This should get you started, if you work though how the code works, the part you need to modify to look for other answers should become obvious.
If you find you are still having trouble, please respond to this this thread and include your Alice movie.
Mark