Jan K wrote:
It is indented. Must be your client.
Wow, my client’s not displaying tabs. That really kinda sucks. Thanks
for
the heads-up!
The problem was that I didn’t realize that I messed up the first
Grandma program (which reinforced some misconceptions because the
program was working ok). Then I sort of got this idea that I had to
use a loop inside a loop and really couldn’t get out of that box.
Everything is obvious now.
Agony of defeat, ecstacy of success.
Yay!
Once you’ve got Flow Control, now you can write programs to do real-life
useful tasks!
Anyway, here’s the final program:
(hopefully I don’t have any superfluous code this time around)
That might be a little much to expect this early in the game
else
number2 = (keep_count + 1)
keep_count = number2
if keep_count != 3
puts ‘HUH?! SPEAK UP, SONNY!’
end
end
end
puts 'NO, NOT SINCE ’ + (1930 + number).to_s + ‘!’
The line is “number2 = 0” is superfluous, because the first thing you do
is
assign to it, but it does serve to make it obvious to the programmer the
variable exists.
If it was my program, though, I’d drop the variables “number” and
“number2”,
and just use the formula directly place - they’re only used once each.
Like
this:
keep_count = keep_count + 1
And like this:
puts 'NO, NOT SINCE ’ + (1930 + rand(21)).to_s + ‘!’
Note you don’t need parentheses around the formula on the right-hand
side of
an assignment: + is done before =.
Finally, I prefer not to use the negative “if x != y then foo else bar”
because I find easier to understand “if x == y then bar else foo”.
Another
alternative is to use “unless”, which Chris seems not to have introduced
in
Chapter 6, but it means “if not”. Same for while - “while keep_count !=
3”
is the same as “until keep_count == 3”.
So here are my changes to your program all together:
keep_count = 0
until keep_count == 3
input = gets.chomp
if input == ‘BYE’
keep_count = keep_count + 1
unless keep_count == 3
puts ‘HUH?! SPEAK UP, SONNY!’
end
else
keep_count = 0
puts ‘HUH?! SPEAK UP, SONNY!’
end
end
puts 'NO, NOT SINCE ’ + (1930 + number).to_s + ‘!’
But, my understanding of the question must be different to yours. I
understand the original should behave like this:
Hello Grandma
HUH?! SPEAK UP, SONNY!
HELLO GRANDMA
NO, NOT SINCE 1938!
WHAT HASN’T BEEN SINCE 1938?
NO, NOT SINCE 1945!
BYE
BYE, BYE
And the 3-times one should be something like:
Hello Grandma
HUH?! SPEAK UP, SONNY!
HELLO GRANDMA
NO, NOT SINCE 1938!
BYE
HUH?! SPEAK UP, SONNY!
BYE
HUH?! SPEAK UP, SONNY!
BYE
BYE, BYE
I’ve written my solutions to these after some whitespace at the end of
this
message.
Cheers,
Dave
Deaf Grandma
input = gets.chomp
until input == “BYE”
if input == input.upcase
puts "NO, NOT SINCE " + (1930 + rand(21)).to_s + “!”
else
puts “HUH!? SPEAK UP, SONNY!”
end
input = gets.chomp
end
puts “BYE, BYE”
Extra Deaf Grandma
byes = 0
input = gets.chomp
until byes == 3
if input == “BYE”
byes = byes + 1
else
byes = 0
end
if input == input.upcase
puts "NO, NOT SINCE " + (1930 + rand(21)).to_s + “!”
else
puts “HUH!? SPEAK UP, SONNY!”
end
input = gets.chomp
end
puts “BYE, BYE”