deaf grandma.

  • Thread starter Houston Barnett-gearhart
  • Start date
M

Martin DeMello

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("<granny> say something")
3 reply = gets.chomp
4 if (reply == reply.upcase)
5 granny_says("<granny> i hear you loud and clear")
6 else
7 granny_says("<granny> eh? speak up!!")
8 end
9 end

Now consider the transcript

<granny> say something
hello
<granny> eh? speak up!!
<granny> say something
i said "hello"
<granny> eh? speak up!!
<granny> say something
"hello"!!!!!!!
<granny> eh? speak up!!
<granny> say something
HELLO!
<granny> i hear you loud and clear
<granny> say something

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

1
2 <granny> say something
3 hello
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 i said "hello"
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 "hello"!!!!!!!
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 HELLO!
4
5 <granny> i hear you loud and clear
2 <granny> 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 <granny> 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:

<granny> say something
"hello"!!!!!!!
<granny> eh? speak up!!
<granny> say something
HELLO!
<granny> i hear you loud and clear
<granny> say something
BYE
<granny> i'm afraid i didn't hear you properly - it almost sounded
like you said 'bye'
<granny> 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
I'm on Windows XP. Recommend away!

I'm on linux, so no real recommendations, I'm afraid. See if
http://rubyonwindows.blogspot.com/2007/05/what-text-editor-should-i-use.html
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
 
J

Jason Morgan

I've been reading Chris Pine'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
 
7

7stud --

Jason said:
I've been reading Chris Pine'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.
 
D

Dave Lilley

houston said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top