PDA

View Full Version : Asking the user to repeat a Loop


ChrisDavidson
07-23-2013, 03:34 AM
So I have my assignment perfect, the lady jumps and spins at the right user entered variable and lands with grace. Now I need to loop it if the user responds yes to the question. I keep leaning to the "while" statement as it will execute based on Yes=true, and I can index the answer "yes" but I can't seem to make this work. Not looking for answers, just advice or small hints. I know it's late, doing this on graveyard shift.

JasonEllis
07-23-2013, 12:21 PM
So I have my assignment perfect, the lady jumps and spins at the right user entered variable and lands with grace. Now I need to loop it if the user responds yes to the question. I keep leaning to the "while" statement as it will execute based on Yes=true, and I can index the answer "yes" but I can't seem to make this work. Not looking for answers, just advice or small hints. I know it's late, doing this on graveyard shift.

I created a variable for the user answer, then did a while statement with a loop nested inside so that while the answer is Yes, yes, or YES, it will loop.

ChrisDavidson
07-24-2013, 01:24 AM
So thank you for the reply. I have it working fine if the user inputs "yes". However it won't stop. I need now to end the "while" statement. Under the while statement I have no=to false, but no matter what the user enters it still goes again.

chickentree
07-24-2013, 08:22 PM
So thank you for the reply. I have it working fine if the user inputs "yes". However it won't stop. I need now to end the "while" statement. Under the while statement I have no=to false, but no matter what the user enters it still goes again.

If I am wrong in my assumptions you need to upload the movie but here goes...

As with everything else in programming there are several ways to do this.
If you go to the world/function tab there is an "Ask user for yes or no" function. Since ask yes no returns a Boolean value (true or false) you need to place it in an instruction which is looking for a Boolean like an If, While, or Set instruction.

While Ask user for yes or no question=Keep Going?
Do Stuff


In this case every time the while block completes, it goes back to the top and puts up the "Keep Going?" message box, gets the users input and either exits or runs the block of instructions under the While again depending on what the user's answer was.

The only problem with this is that the first time through the while loop it stops and asks the user if they want to keep going which is not ideal. We would rather have the while run through once before bothering the user. To accomplish this we use a variable. You can put this variable in the method itself (create new variable top right of method window) and set its default to true. I'll call it fContinue in the code below
Next we change the code as follows:

While fContinue
Do Stuff
fContinue set value to Ask user for yes or no question=Keep Going?


Now when the method starts it sets fContinue to true so when the while loop is evaluated the first time it will enter the while loop. After everything is done in the while but before it ends we shove the fContinue set command. Now the user answers yes or no which sets fContinue to either true or false and the while evaluates fContinue again and either skips the block or runs it again.

Mark