One more Deaf Grandma topic

B

Bianca George

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?? After 2 days, this is driving
me crazy! My code so far is as follows:

speak = ''
bye_count = 0

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

while bye_count != 3
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 < 3
bye_count += 1
puts 'Where ya goin\', Sonny? I\'ve got all day. bye_count = ' +
bye_count.to_s + '.'
speak = gets.chomp
elsif speak == 'bye'.upcase && bye_count == 3
puts 'Fine, Sonny, I gotta take my nap anyway. bye_count = ' +
bye_count.to_s + '.'
end
end

I greatly appreciate any help with this!!

Thanks,
chipsbgz
 
A

Adam Shelly

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.
 
B

Bianca George

Adam said:
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
 
A

Adam Shelly

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:

One way to avoid the break would be to also increment bye_count after
the "nap" line, and change the loop to 'while bye_count < 3'
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?

There are plenty of times where you can and should use 2 or more while
loops in a program. This probably isn't one of them. You really
only have one logical loop:
listen, respond, repeat.... so the program structure should mimic that.

Another tip. You have a lot of redundant lines that could be
eliminated. Eliminating redundancy is a good thing - it generally
makes the program easier to read and update. For instance, instead of
calling gets once before enterting the loop, and again after each
possible response, why not call it right after starting each loop?
while bye_count <= 2 #gets could be here
if speak != speak.upcase
bye_count = 0
puts 'You gotta shout, boy, like THIS.'
speak=gets.chomp #instead of here
elsif speak == speak.upcase && speak != 'bye'.upcase

Also, this elsif line has a completely redundant clause. If the
program gets here, you have already determined that speak ==
speak.upcase (otherwise it would have taken the if branch). You've
done this a number of times - removing them should make the program
easier to follow (and help you see that the second 'No, not since..'
block will never be executed).

-Adam
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top