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

W

woodyee

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
 
A

anne001

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
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top