Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to wrap the entire thing <a href="http://www.techotopia.com/index.php/Ruby_While_and_Until_Loops" rel="nofollow">in a <code>while</code> loop</a> and initialize the variable <code>input</code> to a value like <code>nil</code>. </p> <p>The <code>while</code> loop's condition should check if the value is 1 or 2, and it will probably need to be converted to an integer with <code>.to_i</code> since <code>gets</code> will return a string.</p> <pre><code># initialize to nil input = nil # Check if the current value (integer) is 1 or 2 while !([1,2].include?(input)) puts "Do you want to calculate celcius to fahrenheit (1) or fahrenheit to celcius(2)" # Convert the string to an int after getting it as input input = gets.to_i if input == 1 puts "Please enter degrees in Celcius." celcius = gets fahrenheit = (celcius.to_i * 9 / 5) + 32 print "The result is " print fahrenheit puts "." # elsif here, not elseif!! elsif input == 2 puts "Please enter degrees in Fahrenheit." fahrenheit = gets celcius = (fahrenheit.to_i / 9 * 5) - 32 print "The result is:" print celcius puts "." else puts "Please enter option 1 or 2" end end </code></pre> <p>In fact, rather than a <code>while</code> loop, using an <code>until</code> loop (which Ruby has unlike many other languages) is more readable when testing for a negative condition:</p> <pre><code>until [1,2].include?(input) ... end </code></pre> <p>The <code>[1,2].include?(input)</code> is a slicker way of writing</p> <pre><code> if input == 1 || input == 2 </code></pre> <p>... that is easily expanded for additional values in the array.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload