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