Adam Shelly wrote:
> On 8/19/08, Bianca George <bgz / umich.edu> wrote:
>> Obviously, I'm working through Chris Pine's Learn to Program, and I'm
>> having issues with deaf Grandma. I'm new to programming (hence why I'm
>> reading Learn to Program), and I've gotten as far as getting the program
>> to recognize when 'BYE' has been entered 3 times in a row. But, for some
>> reason, it doesn't enter the last loop correctly and give the proper
>> response once and then stop. Why is this??
> 
> Look closely at what happens when bye_count is 2 and you type 'BYE' 
> again.
> You add 1 to bye_count, then call 'gets', which waits for more input
> before going through the while loop again.

Thanks so much for your help! I managed to write/finish the program 
successfully finally except I had to use a 'break'. Since that wasn't 
covered in Learn to Program before this exercise was introduced, I 
assume that means there is a way to write the program without it, but I 
can't figure it out. Here is what I have:

speak = ''
bye_count = 0'

puts 'Hello, Sonny, how are ya?'
speak = gets.chomp

while bye_count <= 2
  if speak != speak.upcase
    bye_count = 0
    puts 'You gotta shout, boy, like THIS.'
    speak=gets.chomp
  elsif speak == speak.upcase && speak != 'bye'.upcase
    bye_count = 0
    puts 'No, not since ' + (1930+(rand(21))).to_s + '. What else?'
    speak = gets.chomp
  elsif speak == 'bye'.upcase && bye_count < 2
    bye_count += 1
    puts 'Where ya goin\', Sonny? I\'ve got all day. bye_count = ' + 
bye_count.to_s + '.'
    speak = gets.chomp
  elsif bye_count == 2 && speak != 'bye'.upcase
    bye_count = 0
    puts 'No, not since ' + (1930+(rand(21))).to_s + '. What else? 
bye_count =  ' + bye_count.to_s + '.'
    speak = gets.chomp
  elsif bye_count == 2 && speak == 'bye'.upcase
    puts 'Fine, Sonny, I gotta take my nap anyway. bye_count = ' + 
bye_count.to_s + '.'
    break
  end
end



At one point, I had written it with 2 while loops:
    1] while bye_count != 2 (using the first 3 if/elsif's)
    2] while bye_count == 2 (using the last 2 elsif's)
but I couldn't make that work for some reason. Will writing 2 while 
loops in a program work? The problem I was having was either the program 
just terminated without the 'nap' statement or the 'nap' statement ran 
for infinity (I assume that's because bye_count always equaled 2 at that 
point, but I'm not really sure.)

Thanks so much again for your help!
chipsbgz
-- 
Posted via http://www.ruby-forum.com/.