Endless Loop Ruby

I am currently learning ruby ​​and I am stuck with this problem:

Write a deaf grandmother program. No matter what you say to your grandmother (whatever you enter), she should answer HUH ?! SAY, SONNY !, if you do not shout it out (enter all capitals). If you scream, she can hear you and scream back, NO, NOT since 1938! To make your program truly believable, every time your grandmother screams every year; maybe any year in a random order between 1930 and 1950. You cannot stop talking to your grandmother until you start shouting BYE.

This is the code I tried:

puts "Say something to Grandma!" something = gets.chomp while something != "BYE" if something == something.upcase puts "NO, NOT SINCE 19" + (rand(30..50)).to_s + "!" else puts "HUH? SPEAK UP SONNY!" end end 

Whenever I do this, the if and else lines just go into an infinite loop. What am I doing wrong here?

+7
source share
1 answer

You only get the input once, you need to read it at the beginning of each cycle, for example:

 something="" while something != "BYE" puts "Say something to Grandma!" something = gets.chomp if something == something.upcase puts "NO, NOT SINCE 19" + (rand(30..50)).to_s + "!" else puts "HUH? SPEAK UP SONNY!" end end 

Hope this makes sense.

+6
source

All Articles