# Another Deaf Grandma Topic, different issue

**URL:** <https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381>\
**Category:** Ruby\
**Created:** [July 29, 2008, 6:51am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381 "2008-07-29T06:51:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 29, 2008, 6:51am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/1 "2008-07-29T06:51:36Z")

</div>

I’m trying to learn using a tut, and I’m getting stuck on one of the  
exercises. Google is giving me squat.

It’s about Flow Control, specifically while and if-then. I understand it  
totally, but I need, well, it’s hard to explain.

I’ve successfully finished this:

Write a Deaf Grandma program. Whatever you say to grandma (whatever you  
type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you  
shout it (type in all capitals). If you shout, she can hear you (or at  
least she thinks so) and yells back, NO, NOT SINCE 1938! To make your  
program really believable, have grandma shout a different year each  
time; maybe any year at random between 1930 and 1950. (This part is  
optional, and would be much easier if you read the section on Ruby’s  
random number generator at the end of the methods chapter.) You can’t  
stop talking to grandma until you shout BYE.  
Hint: Don’t forget about chomp! 'BYE’with an Enter is not the same as  
‘BYE’ without one!  
Hint 2: Try to think about what parts of your program should happen over  
and over again. All of those should be in your while loop.[/spoiler]

But now I need to do a bit more, and I can’t figure out how to do this:

Extend your Deaf Grandma program: What if grandma doesn’t want you to  
leave? When you shout BYE, she could pretend not to hear you. Change  
your previous program so that you have to shout BYE three times in a  
row. Make sure to test your program: if you shout BYE three times, but  
not in a row, you should still be talking to grandma.

Oh, and here’s my code:

puts ‘it’s yer ol’ gramma hobblin’ over! What do you say?’  
you=gets.chomp

while you!=‘BYE’  
if you!=you.upcase.chomp  
then  
puts ‘HUH?! SPEAK UP, SONNY!’  
you=gets.chomp  
else  
if you==you.upcase.chomp  
then  
puts ‘NO, NOT SINCE 19’+(rand(21)+30).to\_s+’!’  
you=gets.chomp  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 29, 2008, 7:03am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/2 "2008-07-29T07:03:56Z")

</div>

Phee L. wrote:

> I’m trying to learn using a tut, and I’m getting stuck on one of the  
> exercises. Google is giving me squat.
> 
> It’s about Flow Control, specifically while and if-then. I understand it  
> totally, but I need, well, it’s hard to explain.
> 
> I’ve successfully finished this:
> 
> Write a Deaf Grandma program. Whatever you say to grandma (whatever you  
> type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you  
> shout it (type in all capitals). If you shout, she can hear you (or at  
> least she thinks so) and yells back, NO, NOT SINCE 1938! To make your  
> program really believable, have grandma shout a different year each  
> time; maybe any year at random between 1930 and 1950. (This part is  
> optional, and would be much easier if you read the section on Ruby’s  
> random number generator at the end of the methods chapter.) You can’t  
> stop talking to grandma until you shout BYE.  
> Hint: Don’t forget about chomp! 'BYE’with an Enter is not the same as  
> ‘BYE’ without one!  
> Hint 2: Try to think about what parts of your program should happen over  
> and over again. All of those should be in your while loop.[/spoiler]
> 
> But now I need to do a bit more, and I can’t figure out how to do this:
> 
> Extend your Deaf Grandma program: What if grandma doesn’t want you to  
> leave? When you shout BYE, she could pretend not to hear you. Change  
> your previous program so that you have to shout BYE three times in a  
> row. Make sure to test your program: if you shout BYE three times, but  
> not in a row, you should still be talking to grandma.
> 
> Oh, and here’s my code:
> 
> puts ‘it’s yer ol’ gramma hobblin’ over! What do you say?’  
> you=gets.chomp
> 
> while you!=‘BYE’  
> if you!=you.upcase.chomp  
> then  
> puts ‘HUH?! SPEAK UP, SONNY!’  
> you=gets.chomp  
> else  
> if you==you.upcase.chomp  
> then  
> puts ‘NO, NOT SINCE 19’+(rand(21)+30).to\_s+’!’  
> you=gets.chomp  
> end  
> end  
> end

Phee, in the expression for your while loop ( you != ‘BYE’ ), your  
program will exit after precisely 1 ‘BYE’ entered by the user. This  
should be the first place you look, since you don’t want your program to  
exit until there are a total of three ‘BYE’ entries in a row.

My first thought would be to keep a count of how many times the user has  
input ‘BYE’ and check that against 3 ( while bye\_count != 3 ).

Hope that helps.

---

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 29, 2008, 7:15am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/3 "2008-07-29T07:15:31Z")

</div>

Ben Brightwell wrote:

> Phee, in the expression for your while loop ( you != ‘BYE’ ), your  
> program will exit after precisely 1 ‘BYE’ entered by the user. This  
> should be the first place you look, since you don’t want your program to  
> exit until there are a total of three ‘BYE’ entries in a row.
> 
> My first thought would be to keep a count of how many times the user has  
> input ‘BYE’ and check that against 3 ( while bye\_count != 3 ).
> 
> Hope that helps.

I haven’t yet learned count, and so I’m assuming that there’s another  
way to do this. I understand the concept, but how do I define bye?

---

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 29, 2008, 8:10am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/4 "2008-07-29T08:10:12Z")

</div>

Thank you so much!

I renamed it and reworked everything to fit it in.

If you care to take a look, here is my code, reworked. It (seems to, at  
least) work perfectly. 🙂

you = gets.chomp  
byes = 0

while byes != 2  
if you==‘BYE’  
then byes = byes + 1  
else  
byes=0  
end  
if you != you.chomp.upcase  
then  
puts ‘HUH?! SPEAK UP, SONNY!’  
end  
if you == you.chomp.upcase  
then  
puts ‘NO, NOT SINCE 19’+(rand(21)+30).to\_s+’!’  
end  
you = gets.chomp  
end

---

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 29, 2008, 7:24am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/5 "2008-07-29T07:24:40Z")

</div>

On Mon, Jul 28, 2008 at 10:15 PM, Phee L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I haven’t yet learned count, and so I’m assuming that there’s another  
> way to do this. I understand the concept, but how do I define bye?

Just have a variable called bye\_count that contains a number

bye\_count = 0  
puts bye\_count  
bye\_count = bye\_count + 1  
puts bye\_count

martin

---

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 31, 2008, 2:20am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/6 "2008-07-31T02:20:16Z")

</div>

Actually, after thinking about it a bit more, the on/off switch isn’t  
necesary, as that’s what the while loop is for. I don’t think I’ve used  
an if question without an else question before today, so didn’t think  
about it that way. I think that such an on/off switch can be useful in  
some cases (no?), but in this case it wasn’t.

---

<div class="post-metadata">

**Author:** ![phee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@phee](https://www.ruby-forum.com/u/phee)\
**Post date:** [July 31, 2008, 1:59am UTC](https://www.ruby-forum.com/t/another-deaf-grandma-topic-different-issue/144381/7 "2008-07-31T01:59:18Z")

</div>

I think I’ve spent maybe two hours or more solving this, but I finally  
got it in the end… I hope so anyway 🙂 … I have done a bit of a Python  
tutorial a bit ago, so I think I have a bit of more experience than  
Phee, but not much! So don’t yell at me! 🙂

Phee, try inputting “BYE”, “BYE”, “test”. I made the same mistake a  
bunch of times.

Also your indenting is a bit off. Look at the example in the “A Little  
Bit of Logic” section of Chapter 6 to get it right.

I’ve edited your second program to look nicer, comments below:

you = gets.chomp  
byes = 0

while byes != 2  
if you==‘BYE’  
byes += 1  
else  
byes=0  
end  
if you != you.upcase  
puts ‘HUH?! SPEAK UP, SONNY!’  
else  
puts ‘NO, NOT SINCE 19’+(rand(21)+30).to\_s+‘!’  
end  
you = gets.chomp  
end

“byes += 1” is just another way of writing “byes = byes + 1”. This  
haven’t been stated any where in the tutorial though. Nor have the count  
concept as far as I know, but I don’t see how to do the exercise without  
it, unless you do it with rather ugly code.

I’ve removed all the “then” statements as they are unnecessary. I don’t  
know where you picked those up, as I haven’t seen them in the tutorial  
;O … I assume we’re doing the same one (I’m doing this:  
[http://pine.fm/LearnToProgram/](http://pine.fm/LearnToProgram/)).

Anything that isn’t “!= you.chomp.upcase” IS “== you.chomp.upcase”, so I  
could remove that third “if” question as well. Think of it like two  
negatives making it positive (1 - (-1) = 2).

You don’t really need those .chomp’s in the while loop when you do it  
when you get the string (gets). I guess they can be helpful if someone  
forgets to put a chomp on the end of gets, or you only need the string  
to be chomp’ed occasionally, but that isn’t the case here.

What I did to solve it was to make the while loop run until the program  
turned it off, like this (example could be a very personalised music  
player on your pc):

keepPlaying = 1

while keepPlaying != 0  
if  
music\_I\_like\_is\_playing == True  
else  
keepPlaying = 0  
end  
end

Basicly an on/off switch.
