Alice Community  

Go Back   Alice Community > General Discussion > The Lounge

Thread Tools Display Modes
Lab 2 Assignment
Junior Member
Status: Offline
Posts: 6
Join Date: Jan 2012
Default Lab 2 Assignment - 03-11-2012, 08:42 PM

Hello Alice Community,

After working on Lab2 and modifying the world to work properly I will share with you my ideas. I first built an If / Else loop, but noticed it only executed 1 time. Then changed to a While Loop, while the answer to Run again? was Yes, to keep looping program, and it worked. I decided to run statements before the While Loop to have the world start playing and then go into the loop.

I look forward to your comments.

Warm Regards,

Reply With Quote
Status: Offline
Posts: 22
Join Date: Jan 2012
Default 03-14-2012, 12:10 AM

Hi David,
Starting statements before the while loop is one way to go. After re-evaluating my submission for this project, I chose to run all of my statements within my while loop. In fact, to have better error control, I used a while loop within a while loop as follows:

Think of the variables count1 and count2 as on/off switches, 1=on, 2=off. Except these variables kill or start the appropriate while loop.

count1 = 1;
count2 = 2;
spins = 1;
playAgain = default string;

// Start of main loop

while count1 == 1
spins = ask user for a number;
if spins <= 10
character spins around
// Kill main loop
count1 == 2
// Start loop 2
count2 == 1
else if spins > 10
character says "Number is too high"
// Start of loop 2
while count2 == 1
playAgain = "Would you like to play again?";
if playAgain == yes, Yes, or YES
// Restart main loop
count1 == 1
// Kill loop 2
count2 == 2
else if playAgain == no, No, or NO
character says "goodbye!"
// Kill both loops
count == 2
count == 2
character says "I don't understand that answer. Please try again."
// End of loop 2
// End of main loop

This way I was able to control the program in case the user input a number higher than 10 and something other than yes, Yes, YES, or no, No, NO. There are other error controls I could implement such as if the user inputs something other than a number or a negative number ... but this will do for now.

Of course this isn't the only way to do this, but I felt my code was cleaner and easier to work with than trying to nest a whole bunch of if/else statements in one while loop.
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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