# Deaf Grandma

Discussion in 'Ruby' started by danielj, Aug 27, 2008.

1. ### danieljGuest

Also from the Chris Pine tutorial for beginners:

• 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.

• 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.

Here is what I came up with:

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are yeah?"

response = "nope"
bye = 0

while bye < 3
response = gets.chomp
if response == (response.upcase and "BYE")
puts "Hmmm... I would prefer..."
bye = (bye+1)
end
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
end
if (response == response.upcase and response != "BYE")
puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
end
end

Is there an easier way?
danielj, Aug 27, 2008

2. ### Thomas B.Guest

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are yeah?"
response = nil
bye = 0
while bye < 3
response = gets.chomp
if response == "BYE"
puts "Hmmm... I would prefer..."
bye = (bye+1)
elsif response == response.upcase
puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
else
puts "Huh?! I CAN'T HEAR YOU!"
end
end
--
Posted via http://www.ruby-forum.com/.
Thomas B., Aug 27, 2008

3. ### Sebastian HungereckerGuest

danielj wrote:
> if response == (response.upcase and "BYE")

response.upcase and "BYE" evaluates to "BYE" unless response.upcase returns
nil, which it won't. So the above is exactly like if response == "BYE"

--
Jabber:
ICQ: 205544826
Sebastian Hungerecker, Aug 27, 2008
4. ### danieljGuest

Thanks very much for all the help guys.

Programmers seem to be a nice group!
danielj, Aug 27, 2008
5. ### Chris Mr.Guest

danielj wrote:
> Thanks very much for all the help guys.
>
> Programmers seem to be a nice group!

I rewrote some of it like this:

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are you?"

response = gets.chomp
bye = 0

while bye < 1
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
end

if (response == response.upcase and response != "BYE")
puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
end

if response == "BYE"
puts "GOOD BYE, SONNY!"
bye = (bye+1)
end

response = gets.chomp
end

I'm a noob, but this seems cleaner to me. I know there is a way to write
it with out repeating the "if" statements three times but at least it
works The only other thing that bothers me is you have to press enter
at the end to return to the command prompt. If anyone has an example on
how to fix it that would be sweet.
--
Posted via http://www.ruby-forum.com/.
Chris Mr., Oct 21, 2009
6. ### Michael W. RyderGuest

Chris Mr. wrote:
> danielj wrote:
>> Thanks very much for all the help guys.
>>
>> Programmers seem to be a nice group!

>
> I rewrote some of it like this:
>
> #Grandma is deaf!
>
> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>
> response = gets.chomp
> bye = 0
>
> while bye < 1
> if response != response.upcase
> puts "Huh?! I CAN'T HEAR YOU!"
> end
>
> if (response == response.upcase and response != "BYE")
> puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
> end
>
> if response == "BYE"
> puts "GOOD BYE, SONNY!"
> bye = (bye+1)
> end
>
> response = gets.chomp
> end
>
> I'm a noob, but this seems cleaner to me. I know there is a way to write
> it with out repeating the "if" statements three times but at least it
> works The only other thing that bothers me is you have to press enter
> at the end to return to the command prompt. If anyone has an example on
> how to fix it that would be sweet.

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are you?"

while (response = gets.chomp) != "BYE"
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
end

if (response == response.upcase)
puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
end
end
puts "GOOD BYE, SONNY!"

This only uses one entry of response and no flags.
Michael W. Ryder, Oct 21, 2009
7. ### Michael W. RyderGuest

Michael W. Ryder wrote:
> Chris Mr. wrote:
>> danielj wrote:
>>> Thanks very much for all the help guys.
>>>
>>> Programmers seem to be a nice group!

>>
>> I rewrote some of it like this:
>>
>> #Grandma is deaf!
>>
>> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>>
>> response = gets.chomp
>> bye = 0
>>
>> while bye < 1
>> if response != response.upcase
>> puts "Huh?! I CAN'T HEAR YOU!"
>> end
>>
>> if (response == response.upcase and response != "BYE")
>> puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
>> end
>>
>> if response == "BYE"
>> puts "GOOD BYE, SONNY!"
>> bye = (bye+1)
>> end
>>
>> response = gets.chomp
>> end
>>
>> I'm a noob, but this seems cleaner to me. I know there is a way to
>> write it with out repeating the "if" statements three times but at
>> least it works The only other thing that bothers me is you have to
>> press enter at the end to return to the command prompt. If anyone has
>> an example on how to fix it that would be sweet.

>
>
> #Grandma is deaf!
>
> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>
> while (response = gets.chomp) != "BYE"
> if response != response.upcase
> puts "Huh?! I CAN'T HEAR YOU!"
> end
>
> if (response == response.upcase)
> puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
> end
> end
> puts "GOOD BYE, SONNY!"
>
> This only uses one entry of response and no flags.

I missed the part about removing the multiple if's in the oringal post.

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are you?"

while (response = gets.chomp) != "BYE"
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
else
puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
end
end
puts "GOOD BYE, SONNY!"
Michael W. Ryder, Oct 21, 2009
8. ### Chris DowGuest

Michael W. Ryder wrote:
> Michael W. Ryder wrote:
>>> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>>> puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
>>> I'm a noob, but this seems cleaner to me. I know there is a way to

>>
>>
>> This only uses one entry of response and no flags.

>
> I missed the part about removing the multiple if's in the oringal post.
>
> #Grandma is deaf!
>
> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>
> while (response = gets.chomp) != "BYE"
> if response != response.upcase
> puts "Huh?! I CAN'T HEAR YOU!"
> else
> puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
> end
> end
> puts "GOOD BYE, SONNY!"

That one is sweet. Thanks. I guess I was over thinking it, LOL. I didn't
think of putting the first response in parentheses. This helped me think
of it in a much easier way.
--
Posted via http://www.ruby-forum.com/.
Chris Dow, Oct 21, 2009
9. ### Priten G.Guest

After reviewing the previous posts, I noticed that many answers did not
incorporate the condition: '...you have to shout BYE three times in a
row...'

This is what I came up with:

puts 'Hey Sonny, it\'s your Grandma! How are you?'

response = nil
bye = 0

while bye < 3
response = gets.chomp

if response == 'BYE'
bye = (bye + 1)

if bye == 3
puts 'BYE, SONNY!'
else
puts 'HUH?! SPEAK UP, SONNY!'
end

What do you think?

elsif response == response.upcase
puts 'NO! NOT SINCE ' + (1930+rand(21)).to_s + '!'
bye = 0
else
puts 'HUH?! SPEAK UP, SONNY!'
bye = 0
end
end

--
Posted via http://www.ruby-forum.com/.
Priten G., Dec 12, 2010
10. ### Brian CandlerGuest

danielj wrote in post #719787:
> Is there an easier way?

Remember to set your bye counter to zero in every case except where
'BYE' is typed, to ensure three BYE's in a row are required. You can use
'next' to skip straight to the top of the next iteration, which means
you can put a single bye=0 at the end of the loop.

Also, I think there is some ambiguity in the original problem
description: what should happen if what you type contains neither upper
nor lower case letters? (e.g. empty text or only punctuation
characters?). In the code below I've let grandma have a snooze. I also
use the 'case' statement to match the response against a series of
values and regexp patterns.

puts "Hey Sonny, it's your Grandma! How are you?"
bye = 0
loop do
case gets.chomp
when "BYE"
bye += 1
break if bye >= 3
next
when /[a-z]/
puts "HUH?! SPEAK UP, SONNY!"
when /[A-Z]/
puts "NO, NOT SINCE #{rand(21)+1930}!"
else
puts "ZZZ"
end
bye = 0
end

But to be honest, I'd say that if...elsif...elsif...end is equally good.

--
Posted via http://www.ruby-forum.com/.
Brian Candler, Dec 13, 2010
11. ### Steve K.Guest

I was doing stupid things with arrays and all sorts of dumb crap until I =

came here and read this thread. Super good work. I wanted my Ruby script =

to work with the three goodbyes extension exercise mentioned above. =

Also=E2=80=94I like double quotes and #{} to get things done. In my neoph=
yte =

opinion, it's cleaner and less characters.

puts "TALK TO ME! SO LONELY!\n\n"
bye =3D 0
while bye < 3
response =3D gets.chomp
if response =3D=3D "BYE"
puts "STAY AWHILE!?!"
bye =3D (bye+1)
elsif response =3D=3D response.upcase
puts "NO! NOT SINCE #{1910+rand(41)}!"
bye =3D 0
else
puts "Huh?! I CAN'T HEAR YOU!"
bye =3D 0
end
end

If you make it a single string=E2=80=94it's 263 if you format it to one l=
ine and =

strip my OCD new-line characters. It resets "bye" back down to zero if =

it's not said three times straight.

-- =

Posted via http://www.ruby-forum.com/.=
Steve K., May 4, 2011