Deaf grandma

On Tue, Jul 29, 2008 at 2:51 PM, houston barnett-gearhart
[email protected] wrote:

Martin, this keeps being brought up. If I’m understanding correctly,
after the Intermediary explains that questions towards Granny should be
in all caps if you want her to hear you, you want to additionally check
the inputted text (if it’s in all caps) to check if “BYE” has been said.
But, logically that doesn’t make any sense. Why would the first thing
you say to Granny, after it’s been suggested you ask a question, be
“BYE”? It doesn’t make sense to me… which is why I believe I put the

What you’re missing is that, because it’s a loop, the same code gets
executed for every question. Consider the following code

1 loop do
2 granny_says(“ say something”)
3 reply = gets.chomp
4 if (reply == reply.upcase)
5 granny_says(“ i hear you loud and clear”)
6 else
7 granny_says(“ eh? speak up!!”)
8 end
9 end

Now consider the transcript

say something
hello
eh? speak up!!
say something
i said “hello”
eh? speak up!!
say something
“hello”!!!
eh? speak up!!
say something
HELLO!
i hear you loud and clear
say something

Here it is again, annotated with the line of code being run

1
2 say something
3 hello
4
6
7 eh? speak up!!
2 say something
3 i said “hello”
4
6
7 eh? speak up!!
2 say something
3 “hello”!!!
4
6
7 eh? speak up!!
2 say something
3 HELLO!
4
5 i hear you loud and clear
2 say something

Now if the user wants to say ‘bye’, which lines of code will handle
it? Note that the only place where the program prompts for user input
is lines 2 and 3. So we run line 2

2 say something

and wait for user input

3 BYE

the program will go to line 4, then to line 5, then skip 6,7,8 (the
else block) and go to line 9 where it loops again. So the place to
process the user’s input and check if he has said “bye” is inside the
if block. Ideally, instead of

– say “i hear you loud and clear”

you want

– did he say “bye”?
---- was it the third time in a row?
------- if so, exit
------- if not, pretend you didn’t hear

so you want (1) an inner if/else block to replace line 5 and (2) logic
within that block to keep track of whether we’ve seen three
consecutive 'bye’s

The problem with using a separate loop is this:

say something
“hello”!!!
eh? speak up!!
say something
HELLO!
i hear you loud and clear
say something
BYE
i’m afraid i didn’t hear you properly - it almost sounded
like you said ‘bye’
say something
okay, I’m not leaving

Now what? If you’re inside a “wait for three byes” loop, you have no
way of handling something that is not a BYE. So you need to have a
single loop whose overall structure is

loop do
– ask for input
– do something with input
– check if we need to exit
---- if so, exit
---- if not, make a response
end

And use variables for any tracking you need to do that spans multiple
repeats of the loop

If you let us know what platform you are on, people can recommend their >favourite editors and IDEs

I’m on Windows XP. Recommend away!

I’m on linux, so no real recommendations, I’m afraid. See if

has anything helpful to suggest. Also, people’s requirements vary; for
me, autoindentation is the key feature of a good programming editor,
followed by decent syntax highlighting.

martin

There’s a problem with your code as written - note that once you’ve
said “bye” you can never say anything else - it sticks in the loop
till it counts three ‘byes’ in a row. Whereas what you want is to exit
the whole loop if you see three byes in a row, otherwise just carry on
as normal. What you should do is this -

if the user has yelled, so that granny hears what he says, do an
additional check for his having said “bye”.
---- If that is true, then increase the bye count.
-------- If the bye count is 3, exit the loop
---- If that is false, reset the bye count to zero and carry on with
the rest of the code

Martin, this keeps being brought up. If I’m understanding correctly,
after the Intermediary explains that questions towards Granny should be
in all caps if you want her to hear you, you want to additionally check
the inputted text (if it’s in all caps) to check if “BYE” has been said.
But, logically that doesn’t make any sense. Why would the first thing
you say to Granny, after it’s been suggested you ask a question, be
“BYE”? It doesn’t make sense to me… which is why I believe I put the
loop where it lays currently. Of course, I could be completely
misunderstanding what you’re saying. Which is more likely! If I am, just
clarify & I’ll begin working on what you’ve been trying to say. At it’s
written, it works as I want it to. It is still heavily rooted in Chris
Pine’s exercise & covers most, if not all, of the requirements. The one
thing that I believe is most significant is the loop issue, which I hope
to have sorted out later today if all goes well.

If you let us know what platform you are on, people can recommend their >favourite editors and IDEs

I’m on Windows XP. Recommend away!

I’ve been reading Chris P.'s book the last couple of days. I spent
most the day today stuck on this deaf Grandma thing.

This is what i finally came up with hope it helps;

count = 1

while count != 4
response = gets.chomp

if response == ‘BYE’
count = count + 1
else
count = 1
end

if response == response.upcase && response != ‘BYE’
puts 'Just like in ’ + (rand(30) + 1920).to_s
end

if response == response.downcase
puts ‘HUH?! SPEAK UP, SONNY!’
end
end

Jason Morgan wrote:

I’ve been reading Chris P.'s book the last couple of days. I spent
most the day today stuck on this deaf Grandma thing.

This is what i finally came up with hope it helps;

Finally! After 8 long months of checking this thread every day, someone
posts a solution. Thank you, thank you, thank you.

houston barnett-gearhart wrote:

Martin, is there any way I can get in touch with you outside of the
forum? Preferably through a medium that is as close to real-time as
possible, i.e., Skype, Gmail’s chat function, etc. I appreciate the help
you’ve given me thus far & would like to be able to bounce my progress
off you as I work on the exercise, so as to be able to ascertain whether
or not I’m on the right track. This will undoubtedly help me
significantly, alongside being a bit more time efficient, considering I
spent a considerable amount of time on the code I posted & I was on the
wrong track from the get-go. I think it’d be easier this way, not to
mention space & time efficient.

Sorry for the interuption but you may find the RubyLearning Free online
course useful.

3 months duration no fee only your time and commitment to join
discussions, try writing programs and call for help when stuck.

url is www.rubylearning.com

sorry to those who i might offend

dave.