NOTE: I posted this in the RForum category accidentally, and it seems
very dead there so here I am.
Yeah, I know. Very bad topic title, but there is no quick way to
describe this problem in 2 words:
I am learning Ruby right now, and I am using the wonderful Learning R.
guide by Satish T… I am currently at the chapter “Random Numbers”
and I am trying to do the following:
This assignment is from Chris P.'s Book.
- 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. You can’t
stop talking to grandma until you shout BYE. - 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.
I have done all of that, but I have one problem. I am using SciTE, and
am using the F5 thing to try out my code. My one problem is that when I
run the program, the correct text comes on, but I have to press enter
twice for the while loop to start. Sounds confusing, I know, so here is
my code:
bye_response = 0
years = (1930…1950).to_a
puts “'Ey sonny! Long time no chat with your old nonna, eh? How ya
been!?”
STDOUT.flush
response = gets.chomp
while bye_response != 3
response = gets.chomp
if bye_response == 2
puts “FINE! LEAVE YOUR OLD NONNA!”
bye_response += 1
elsif response == “BYE”
puts “HUH!? WHAT WAS THAT!?”
bye_response += 1
elsif response == response.upcase
puts "NO, NOT SINCE " + years[rand(years.max - years.min)].to_s +
“!”
bye_response = 0
else
puts “HUH?! SPEAK UP, SONNY!”
bye_response = 0
end
STDOUT.flush
end
Try it out in SciTE and you’ll see what I mean. If you don’t have SciTE
and still want to see what I mean, I took a screenshot:
http://www.michaelboutros.com/uploads/ruby.jpg. Thanks a lot!