From: Jan_K Date: 2006-03-30T12:18:43+09:00 Subject: Re: Learn to Program, by Chris Pine On Thu, 30 Mar 2006 07:03:17 +0900, "Jeppe Jakobsen" wrote: >forgot to make it set counter to 0 if it wasn't "BYE", but I'm sure you can >figure that out using else. > >2006/3/29, Jeppe Jakobsen : >> >> You could use until or if (or both). You could make a variable the would >> count how many times in a row you said "BYE" to grandma, starting at 0 of >> cause, and make it add 1 each times and reset it to 0 if you did not say >> "BYE". >> >> Here is an example: >> >> counter = 0 >> >> Until (counter == 3) >> tell_grandma = gets.chomp >> if (tell_grandma == "BYE") >> counter += 1 >> end >> end >> >> Just find a way to integrate with your current grandma program :) I can't use "Until" because it wasn't covered in the book so far (even though it seems pretty straightforward what it does). Ditto for "+-" What was covered so far up until this chapter: if, else, elsif, while The first thing I tried was nesting IF's and WHILE's in different combinations without using a variable to track the "BYE"s. I thought that this would be the elegant solution, but really couldn't come up with a way to do it. So then I falled back to my first idea of "use variable to track BYE's." But it looks like I'm not understanding the rules of nesting and when/how a branch finishes and kicks you back to the previous branch. Here's what I had when I gave up: ------------------------------------------ input = gets.chomp number = rand(21) number2 = 0 keep_count = 0 if keep_count != 3 while input != 'BYE' puts 'HUH?! SPEAK UP, SONNY!' keep_count = 0 input = gets.chomp while input == 'BYE' puts 'HUH?! SPEAK UP, SONNY!' number2 = (keep_count + 1) keep_count = number2 input = gets.chomp end end else puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!' end ----------------------------------------- And here's the regular Deaf Grandma solution that I used: ---------------------------------------- input = gets.chomp number = rand(21) if while input != 'BYE' puts 'HUH?! SPEAK UP, SONNY!' input = gets.chomp end else puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!' end ------------------------------------- Thanks. -- Jan