control flow - Test for repeated gets.chomp input in Ruby -
i working in ruby, , trying test user's input (the variable kid) being same 3 times.
i want method speak called on endlessly until user enters "bye" 3 separate times when asked 3 separate questions.
right now, if user inputs "bye" 1 question entire conversation between terminal , user ends.
is there way have program test "bye" being said 3 times, , once said 3 separate times, have conversation end?
kid = gets.chomp unless kid == "bye" speak end i don't know if there simple solution or complex one, answers help.
you need keep track of how many times user has input "bye". when number reaches 3 exit:
byecount = 0 while kid = gets.chomp case kid when "bye" #increase count of bye byecount +=1 puts byecount break if byecount == 3 else #reset our count byecount = 0 speak end end the code resets 0 on non "bye" answer.
i find case statement handy in situations when dealing different user input, coupled regular expressions.
Comments
Post a Comment