Morton Goldberg
12/8/2007 2:25:00 AM
On Dec 7, 2007, at 2:12 PM, Nigama Xx wrote:
> The first question I had was, for some reason, if you input "BYE!" for
> the second or third required "BYE" (to exit/end the program), it
> launches into an infinite loop. Why is that? BYE works correctly.
> What is it about "BYE!"?
<snip>
> if talk == 'BYE'
> puts 'EYE? WHAT\'S WRONG WITH YOUR EYE?'
> talk = gets.chomp
> while talk != 'BYE'
> if talk != talk.upcase
> puts 'HUH?! SPEAK UP, SONNY!'
> elsif talk == talk.upcase && talk != 'BYE'
> year = 1
> while year < 30
> year = rand(51)
> end
> puts 'NO, NOT SINCE 19' + year.to_s + '!'
> end
> end
>
> if talk == 'BYE'
> puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
> end
> end
Nothing is really special about "BYE!". Because you don't ask for
input within the while-loop, any response other than "BYE" will put
it into an infinite loop. The following revision will work.
<code>
if talk == 'BYE'
puts 'EYE? WHAT\'S WRONG WITH YOUR EYE?'
talk = gets.chomp
while talk != 'BYE'
if talk != talk.upcase
puts 'HUH?! SPEAK UP, SONNY!'
elsif talk == talk.upcase && talk != 'BYE'
year = 1
while year < 30
year = rand(51)
end
puts 'NO, NOT SINCE 19' + year.to_s + '!'
end
talk = gets.chomp # <--- new code here
end
if talk == 'BYE'
puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
end
end
</code>
Also you might be interested in simplifying your code -- for example:
<code>
if talk == 'BYE'
puts 'EYE? WHAT\'S WRONG WITH YOUR EYE?'
talk = gets.chomp
while talk != 'BYE'
if talk != talk.upcase
puts 'HUH?! SPEAK UP, SONNY!'
else
puts "NO, NOT SINCE #{1930 + rand(21)}!"
end
talk = gets.chomp
end
puts 'OH VERY WELL, SEE YOU ANOTHER TIME, THEN!'
end
</code>
> Second question is... what if I wanted the program to require you
> to say
> "BYE" three times in a row instead of just three times throughout the
> conversation. Is there any easy way to do this?
Depends what you mean by easy. I don't think it would be difficult,
but it would require a fair amount of rewriting. Don't have the time
now to go into the details. I'm sorry.
Regards, Morton