# One more Deaf Grandma topic

**URL:** <https://www.ruby-forum.com/t/one-more-deaf-grandma-topic/146312>\
**Category:** Ruby\
**Created:** [August 19, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/one-more-deaf-grandma-topic/146312 "2008-08-19T23:40:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chipsbgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@chipsbgz](https://www.ruby-forum.com/u/chipsbgz)\
**Post date:** [August 19, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/one-more-deaf-grandma-topic/146312/1 "2008-08-19T23:40:23Z")

</div>

Obviously, I’m working through Chris P.'s Learn to Program, and I’m  
having issues with deaf Grandma. I’m new to programming (hence why I’m  
reading Learn to Program), and I’ve gotten as far as getting the program  
to recognize when ‘BYE’ has been entered 3 times in a row. But, for some  
reason, it doesn’t enter the last loop correctly and give the proper  
response once and then stop. Why is this?? After 2 days, this is driving  
me crazy! My code so far is as follows:

speak = ‘’  
bye\_count = 0

puts ‘Hello, Sonny, how are ya?’  
speak = gets.chomp

while bye\_count != 3  
if speak != speak.upcase  
bye\_count = 0  
puts ‘You gotta shout, boy, like THIS.’  
speak=gets.chomp  
elsif speak == speak.upcase && speak != ‘bye’.upcase  
bye\_count = 0  
puts 'No, not since ’ + (1930+(rand(21))).to\_s + ‘. What else?’  
speak = gets.chomp  
elsif speak == ‘bye’.upcase && bye\_count \< 3  
bye\_count += 1  
puts ‘Where ya goin’, Sonny? I’ve got all day. bye\_count = ’ +  
bye\_count.to\_s + ‘.’  
speak = gets.chomp  
elsif speak == ‘bye’.upcase && bye\_count == 3  
puts 'Fine, Sonny, I gotta take my nap anyway. bye\_count = ’ +  
bye\_count.to\_s + ‘.’  
end  
end

I greatly appreciate any help with this!!

Thanks,  
chipsbgz

---

<div class="post-metadata">

**Author:** ![chipsbgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@chipsbgz](https://www.ruby-forum.com/u/chipsbgz)\
**Post date:** [August 20, 2008, 1:33am UTC](https://www.ruby-forum.com/t/one-more-deaf-grandma-topic/146312/2 "2008-08-20T01:33:09Z")

</div>

On 8/19/08, Bianca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Obviously, I’m working through Chris P.'s Learn to Program, and I’m  
> having issues with deaf Grandma. I’m new to programming (hence why I’m  
> reading Learn to Program), and I’ve gotten as far as getting the program  
> to recognize when ‘BYE’ has been entered 3 times in a row. But, for some  
> reason, it doesn’t enter the last loop correctly and give the proper  
> response once and then stop. Why is this??

Look closely at what happens when bye\_count is 2 and you type ‘BYE’  
again.  
You add 1 to bye\_count, then call ‘gets’, which waits for more input  
before going through the while loop again.

---

<div class="post-metadata">

**Author:** ![chipsbgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@chipsbgz](https://www.ruby-forum.com/u/chipsbgz)\
**Post date:** [August 20, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/one-more-deaf-grandma-topic/146312/3 "2008-08-20T20:38:30Z")

</div>

Adam S. wrote:

> On 8/19/08, Bianca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Obviously, I’m working through Chris P.'s Learn to Program, and I’m  
> > having issues with deaf Grandma. I’m new to programming (hence why I’m  
> > reading Learn to Program), and I’ve gotten as far as getting the program  
> > to recognize when ‘BYE’ has been entered 3 times in a row. But, for some  
> > reason, it doesn’t enter the last loop correctly and give the proper  
> > response once and then stop. Why is this??
> 
> Look closely at what happens when bye\_count is 2 and you type ‘BYE’  
> again.  
> You add 1 to bye\_count, then call ‘gets’, which waits for more input  
> before going through the while loop again.

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:

speak = ‘’  
bye\_count = 0’

puts ‘Hello, Sonny, how are ya?’  
speak = gets.chomp

while bye\_count \<= 2  
if speak != speak.upcase  
bye\_count = 0  
puts ‘You gotta shout, boy, like THIS.’  
speak=gets.chomp  
elsif speak == speak.upcase && speak != ‘bye’.upcase  
bye\_count = 0  
puts 'No, not since ’ + (1930+(rand(21))).to\_s + ‘. What else?’  
speak = gets.chomp  
elsif speak == ‘bye’.upcase && bye\_count \< 2  
bye\_count += 1  
puts 'Where ya goin', Sonny? I've got all day. bye\_count = ’ +  
bye\_count.to\_s + ‘.’  
speak = gets.chomp  
elsif bye\_count == 2 && speak != ‘bye’.upcase  
bye\_count = 0  
puts 'No, not since ’ + (1930+(rand(21))).to\_s + '. What else?  
bye\_count = ’ + bye\_count.to\_s + ‘.’  
speak = gets.chomp  
elsif bye\_count == 2 && speak == ‘bye’.upcase  
puts 'Fine, Sonny, I gotta take my nap anyway. bye\_count = ’ +  
bye\_count.to\_s + ‘.’  
break  
end  
end

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? The problem I was having was either the program  
just terminated without the ‘nap’ statement or the ‘nap’ statement ran  
for infinity (I assume that’s because bye\_count always equaled 2 at that  
point, but I’m not really sure.)

Thanks so much again for your help!  
chipsbgz

---

<div class="post-metadata">

**Author:** ![chipsbgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@chipsbgz](https://www.ruby-forum.com/u/chipsbgz)\
**Post date:** [August 21, 2008, 1:16am UTC](https://www.ruby-forum.com/t/one-more-deaf-grandma-topic/146312/4 "2008-08-21T01:16:15Z")

</div>

On 8/20/08, Bianca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
