Adam Shelly
8/20/2008 11:12:00 PM
On 8/20/08, Bianca George <bgz@umich.edu> wrote:
>
> Thanks so much for your help! I managed to write/finish the program
> successfully finally except I had to use a 'break'. Since that wasn't
> covered in Learn to Program before this exercise was introduced, I
> assume that means there is a way to write the program without it, but I
> can't figure it out. Here is what I have:
One way to avoid the break would be to also increment bye_count after
the "nap" line, and change the loop to 'while bye_count < 3'
> At one point, I had written it with 2 while loops:
> 1] while bye_count != 2 (using the first 3 if/elsif's)
> 2] while bye_count == 2 (using the last 2 elsif's)
> but I couldn't make that work for some reason. Will writing 2 while
> loops in a program work?
There are plenty of times where you can and should use 2 or more while
loops in a program. This probably isn't one of them. You really
only have one logical loop:
listen, respond, repeat.... so the program structure should mimic that.
Another tip. You have a lot of redundant lines that could be
eliminated. Eliminating redundancy is a good thing - it generally
makes the program easier to read and update. For instance, instead of
calling gets once before enterting the loop, and again after each
possible response, why not call it right after starting each loop?
> while bye_count <= 2
#gets could be here
> if speak != speak.upcase
> bye_count = 0
> puts 'You gotta shout, boy, like THIS.'
> speak=gets.chomp
#instead of here
> elsif speak == speak.upcase && speak != 'bye'.upcase
Also, this elsif line has a completely redundant clause. If the
program gets here, you have already determined that speak ==
speak.upcase (otherwise it would have taken the if branch). You've
done this a number of times - removing them should make the program
easier to follow (and help you see that the second 'No, not since..'
block will never be executed).
-Adam