Mads Enoch Mads Enoch - 10 months ago 171
Ruby Question

How to do loop in ruby

I need help putting this question in a loop:

puts "Rate the requirements and quality on a scale from 1-10 points."
x = gets.chomp.to_f
raq = 3318.6 * 1.5066**x
puts "This will cost " + raq.to_s + "kr."


If the user answers anything other than 1 through 10, they must be asked the question again. I have many questions in a row, so I would appreciate not having the whole program restart but just the single question.

Answer Source

Something like this should work:

x = nil

until (1..10).include? x
  puts "Rate the requirements and quality on a scale from 1-10 points."
  x = gets.to_f
end

raq = 3318.6*1.5066**x 

puts "This will cost #{raq}kr."

(Personally, I'd try to use something a little more descriptive than x :) )

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download