0

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.

sawa
  • 165,429
  • 45
  • 277
  • 381
Mads Enoch
  • 13
  • 1
  • 1
    Have you read on loops in ruby? If not, go read that. If yes, what specific problems are you encountering? – Sergio Tulentsev Nov 24 '17 at 13:25
  • @SergioTulentsev This is what i tried, but i keep asking the questions no matter what i wrote. while x!=(1..10).to_s puts "Rate the requirements and quality on a scale from 1-10 points." x = gets.chomp.to_f end – Mads Enoch Nov 24 '17 at 13:27
  • Possible duplicate of [Syntax for a for loop in ruby](https://stackoverflow.com/questions/2032875/syntax-for-a-for-loop-in-ruby) – Nebril Nov 24 '17 at 13:28
  • @MadsEnoch please edit your question instead. – Stefan Nov 24 '17 at 13:29

1 Answers1

1

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 :) )

SRack
  • 11,495
  • 5
  • 47
  • 60
  • 2
    You should remove the redundant `chomp`. Also, better to use string interpolation and get rid of `to_s` on the last line. – sawa Nov 24 '17 at 13:33
  • 1
    Fair enough - I just added the `until` loop in there and used the rest of the code from the question. Makes sense to have it nice and clean though. Will edit @sawa – SRack Nov 24 '17 at 13:34
  • Ofcourse, until. Totally forgot that existed, thank you! – Mads Enoch Nov 24 '17 at 13:34