Prompting user for input until they provide an answer


#1

I'm trying to make the program ask for an answer until they answer it.

Would this work for that?

print "Please type any word:"
user_input = gets.chomp
user_input.capitalize!

until user_input.length > 0

print "You forgot to type something:"

end

if user_input.include? ("s" || "S") || ("ci" || "Ci") || ("ce" || "Ce")

user_input.gsub!(/s/, "th")
user_input.gsub!(/S/, "th")
user_input.gsub!(/ci/, "thi")
user_input.gsub!(/Ci/, "thi")
user_input.gsub!(/ce/, "the")
user_input.gsub!(/Ce/, "the")

puts "Duffy says: #{user_input}"

else

print "No Duffy Duck for #{user_input}"

end


#2

Nope. Just tried it. It's an infinite loop. Suggestions.


#3

An infinite loop is kind of what you want, since you want the user to be asked for input again and again until he can pass the condition.

However, in your loop, there's only a print and nothing else. What do you need to add inside so that the user is asked for input an infinite amount of times?


#4

Aren't the boolean operators ineffective in this scenario, because if the first expression on the left is met (user_input includes "s") it will not test the expression to the right (user_input includes "ci")?


#5

Actually in this scenario any kind of condition is unnecessary, since we have no requirement other than replacing letters. If we had more strict requirements, we could need conditions, but as it is for all we care we could basically just get rid of the if statement.


#6

Okay thanks! I thought the conditions learned in the earlier lesson applied in all cases. Cool!


#7

Adding in

elsif user_input#empty?
print "Please enter something"

works for the first part, but it screws up the final output.

I am stuck!


#8

This is just the loop part. I think it's working! =)

print "Type something"

user = gets.chomp

until user != "" do

    print "Type again"

    user = gets.chomp

end