Another Deaf Grandma Topic, different issue

Discussion in 'Ruby' started by Phee Luche, Jul 29, 2008.

  1. Phee Luche

    Phee Luche Guest

    I'm trying to learn using a tut, and I'm getting stuck on one of the
    exercises. Google is giving me squat.

    It's about Flow Control, specifically while and if-then. I understand it
    totally, but I need, well, it's hard to explain.

    I've successfully finished this:

    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. (This part is
    optional, and would be much easier if you read the section on Ruby's
    random number generator at the end of the methods chapter.) You can't
    stop talking to grandma until you shout BYE.
    Hint: Don't forget about chomp! 'BYE'with an Enter is not the same as
    'BYE' without one!
    Hint 2: Try to think about what parts of your program should happen over
    and over again. All of those should be in your while loop.[/spoiler]

    But now I need to do a bit more, and I can't figure out how to do this:

    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.

    Oh, and here's my code:

    puts 'it\'s yer ol\' gramma hobblin\' over! What do you say?'
    you=gets.chomp


    while you!='BYE'
    if you!=you.upcase.chomp
    then
    puts 'HUH?! SPEAK UP, SONNY!'
    you=gets.chomp
    else
    if you==you.upcase.chomp
    then
    puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
    you=gets.chomp
    end
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
    Phee Luche, Jul 29, 2008
    #1
    1. Advertising

  2. Phee Luche wrote:
    > I'm trying to learn using a tut, and I'm getting stuck on one of the
    > exercises. Google is giving me squat.
    >
    > It's about Flow Control, specifically while and if-then. I understand it
    > totally, but I need, well, it's hard to explain.
    >
    > I've successfully finished this:
    >
    > 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. (This part is
    > optional, and would be much easier if you read the section on Ruby's
    > random number generator at the end of the methods chapter.) You can't
    > stop talking to grandma until you shout BYE.
    > Hint: Don't forget about chomp! 'BYE'with an Enter is not the same as
    > 'BYE' without one!
    > Hint 2: Try to think about what parts of your program should happen over
    > and over again. All of those should be in your while loop.[/spoiler]
    >
    > But now I need to do a bit more, and I can't figure out how to do this:
    >
    > 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.
    >
    > Oh, and here's my code:
    >
    > puts 'it\'s yer ol\' gramma hobblin\' over! What do you say?'
    > you=gets.chomp
    >
    >
    > while you!='BYE'
    > if you!=you.upcase.chomp
    > then
    > puts 'HUH?! SPEAK UP, SONNY!'
    > you=gets.chomp
    > else
    > if you==you.upcase.chomp
    > then
    > puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
    > you=gets.chomp
    > end
    > end
    > end


    Phee, in the expression for your while loop ( you != 'BYE' ), your
    program will exit after precisely 1 'BYE' entered by the user. This
    should be the first place you look, since you don't want your program to
    exit until there are a total of three 'BYE' entries in a row.

    My first thought would be to keep a count of how many times the user has
    input 'BYE' and check that against 3 ( while bye_count != 3 ).

    Hope that helps.
    --
    Posted via http://www.ruby-forum.com/.
    Ben Brightwell, Jul 29, 2008
    #2
    1. Advertising

  3. Phee Luche

    Phee Luche Guest

    Ben Brightwell wrote:
    >
    > Phee, in the expression for your while loop ( you != 'BYE' ), your
    > program will exit after precisely 1 'BYE' entered by the user. This
    > should be the first place you look, since you don't want your program to
    > exit until there are a total of three 'BYE' entries in a row.
    >
    > My first thought would be to keep a count of how many times the user has
    > input 'BYE' and check that against 3 ( while bye_count != 3 ).
    >
    > Hope that helps.


    I haven't yet learned count, and so I'm assuming that there's another
    way to do this. I understand the concept, but how do I define bye?
    --
    Posted via http://www.ruby-forum.com/.
    Phee Luche, Jul 29, 2008
    #3
  4. On Mon, Jul 28, 2008 at 10:15 PM, Phee Luche <> wrote:
    >
    > I haven't yet learned count, and so I'm assuming that there's another
    > way to do this. I understand the concept, but how do I define bye?


    Just have a variable called bye_count that contains a number

    bye_count = 0
    puts bye_count
    bye_count = bye_count + 1
    puts bye_count

    martin
    Martin DeMello, Jul 29, 2008
    #4
  5. Phee Luche

    Phee Luche Guest

    Thank you so much!

    I renamed it and reworked everything to fit it in.

    If you care to take a look, here is my code, reworked. It (seems to, at
    least) work perfectly. :)

    you = gets.chomp
    byes = 0

    while byes != 2
    if you=='BYE'
    then byes = byes + 1
    else
    byes=0
    end
    if you != you.chomp.upcase
    then
    puts 'HUH?! SPEAK UP, SONNY!'
    end
    if you == you.chomp.upcase
    then
    puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
    end
    you = gets.chomp
    end
    --
    Posted via http://www.ruby-forum.com/.
    Phee Luche, Jul 29, 2008
    #5
  6. Phee Luche

    Simon Anker Guest

    I think I've spent maybe two hours or more solving this, but I finally
    got it in the end.. I hope so anyway :) .. I have done a bit of a Python
    tutorial a bit ago, so I think I have a bit of more experience than
    Phee, but not much! So don't yell at me! :)

    Phee, try inputting "BYE", "BYE", "test". I made the same mistake a
    bunch of times.

    Also your indenting is a bit off. Look at the example in the "A Little
    Bit of Logic" section of Chapter 6 to get it right.

    I've edited your second program to look nicer, comments below:

    you = gets.chomp
    byes = 0

    while byes != 2
    if you=='BYE'
    byes += 1
    else
    byes=0
    end
    if you != you.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
    else
    puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
    end
    you = gets.chomp
    end


    "byes += 1" is just another way of writing "byes = byes + 1". This
    haven't been stated any where in the tutorial though. Nor have the count
    concept as far as I know, but I don't see how to do the exercise without
    it, unless you do it with rather ugly code.

    I've removed all the "then" statements as they are unnecessary. I don't
    know where you picked those up, as I haven't seen them in the tutorial
    ;O .. I assume we're doing the same one (I'm doing this:
    http://pine.fm/LearnToProgram/).

    Anything that isn't "!= you.chomp.upcase" IS "== you.chomp.upcase", so I
    could remove that third "if" question as well. Think of it like two
    negatives making it positive (1 - (-1) = 2).

    You don't really need those .chomp's in the while loop when you do it
    when you get the string (gets). I guess they can be helpful if someone
    forgets to put a chomp on the end of gets, or you only need the string
    to be chomp'ed occasionally, but that isn't the case here.


    What I did to solve it was to make the while loop run until the program
    turned it off, like this (example could be a very personalised music
    player on your pc):

    keepPlaying = 1

    while keepPlaying != 0
    if
    music_I_like_is_playing == True
    else
    keepPlaying = 0
    end
    end

    Basicly an on/off switch.
    --
    Posted via http://www.ruby-forum.com/.
    Simon Anker, Jul 31, 2008
    #6
  7. Phee Luche

    Simon Anker Guest

    Actually, after thinking about it a bit more, the on/off switch isn't
    necesary, as that's what the while loop is for. I don't think I've used
    an if question without an else question before today, so didn't think
    about it that way. I think that such an on/off switch can be useful in
    some cases (no?), but in this case it wasn't.
    --
    Posted via http://www.ruby-forum.com/.
    Simon Anker, Jul 31, 2008
    #7
    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:
    263
    Dave Lilley
    Mar 19, 2009
  3. Bianca George

    One more Deaf Grandma topic

    Bianca George, Aug 19, 2008, in forum: Ruby
    Replies:
    3
    Views:
    194
    Adam Shelly
    Aug 21, 2008
  4. danielj

    Deaf Grandma

    danielj, Aug 27, 2008, in forum: Ruby
    Replies:
    10
    Views:
    195
    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