Help with Exercise from Pine's Learning to Program (Chapter 7)

Discussion in 'Ruby' started by woodyee, May 2, 2006.

  1. woodyee

    woodyee Guest

    Help! I'm stuck! I'm using Chris Pine's book and I'm stuck on the first
    exercise at the end of chapter 7 ("Deaf Grandma"). Can someone offer
    suggestions? I can get the first part (ask, she yells 'speak up' unless
    I ask in caps in that case she'll say 'no...') but I'm lost after that.
    Thanks so much!

    #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!
    #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.

    ask = gets.chomp
    while ask != ask.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
    ask = gets.chomp
    if ask = ask.upcase
    puts 'NO! NOT SINCE 1938'
    ask = gets.chomp
    end
    if puts 'BYE'
    ask = gets.chomp
    end
    end
    woodyee, May 2, 2006
    #1
    1. Advertising

  2. woodyee

    anne001 Guest

    Is this your code?
    if ask = ask.upcase

    shouldn't it be if ask == ask.upcase

    have you seen case so far?
    it seems to me I would do in pseudocode
    loop
    get the ask string
    case(not uppercase)
    speak up
    case(BYE)
    break out of loop
    case (all other cases)
    no not since ...
    end cases
    endloop

    To break out of loop, you can set a variable
    say wanttostop=0
    while(wanttostop==0)
    cases...
    case BYE
    wanttostop=1
    end

    or there are ways to break out of a loop, don't know how much the book
    has covered.

    Does this help?
    You can use if same as case, if you have not seen case
    anne001, May 2, 2006
    #2
    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. woodyee
    Replies:
    10
    Views:
    173
    Gregor Kopp
    Feb 28, 2006
  2. Randy Shipp
    Replies:
    1
    Views:
    102
    SonOfLilit
    Feb 19, 2007
  3. Replies:
    9
    Views:
    396
    whisperjim
    Nov 27, 2008
  4. Replies:
    0
    Views:
    247
  5. Vegard Sandengen
    Replies:
    5
    Views:
    310
    Josh Cheek
    Feb 4, 2010
Loading...

Share This Page