One more Deaf Grandma topic

Discussion in 'Ruby' started by Bianca George, Aug 19, 2008.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
    Bianca George, Aug 19, 2008
    #1
    1. Advertising

  2. Bianca George

    Adam Shelly Guest

    On 8/19/08, Bianca George <> 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.
    Adam Shelly, Aug 20, 2008
    #2
    1. Advertising

  3. Adam Shelly wrote:
    > On 8/19/08, Bianca George <> 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/.
    Bianca George, Aug 20, 2008
    #3
  4. Bianca George

    Adam Shelly Guest

    On 8/20/08, Bianca George <> wrote:
    >
    > 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
    Adam Shelly, Aug 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trans
    Replies:
    12
    Views:
    213
    Trans
    Nov 18, 2005
  2. Houston Barnett-gearhart

    deaf grandma.

    Houston Barnett-gearhart, Jul 27, 2008, in forum: Ruby
    Replies:
    23
    Views:
    265
    Dave Lilley
    Mar 19, 2009
  3. Phee Luche
    Replies:
    6
    Views:
    176
    Simon Anker
    Jul 31, 2008
  4. danielj

    Deaf Grandma

    danielj, Aug 27, 2008, in forum: Ruby
    Replies:
    10
    Views:
    197
    Steve K.
    May 4, 2011
  5. Luke Kyrchner

    Deaf Calculus

    Luke Kyrchner, Jun 4, 2009, in forum: Ruby
    Replies:
    4
    Views:
    78
    Todd Benson
    Jun 5, 2009
Loading...

Share This Page