How to get the input from

I want to know, what code i use, if someone doesnt enter the correct
word/phrase, lets say (using my script). I want to know how if they
enter something wrong like at the begining if they enter K_sdg_C, and
how do i put something like pick either bleh or bleh

print “For Kelvin to Celcius type in K_to_C, for Celcius to Kelvins type
in C_to_K, which one do you wanna do?”
choice = gets.chomp
if choice == “K_to_C”;
print “Type in Kelvin Tempurature.”
kelvin = gets.chomp
kelvin = kelvin.to_f
puts (kelvin - 273)
end
if choice == “C_to_K”;
print “Type in Celcius Tempurature.”
Celcius = gets.chomp.to_i
celcius = celcius.to_f
puts (Celcius + 273)
end

I’m a bit new to Ruby, but in other langs a common approach is to put
your question in a while loop.

while (true) do
print “Last night was I awful, okay, good, great, or fantastic?”
choice = gets.chomp
if choice == “great”
break
end
if choice == “fantastic”
break
end
end

I didnt run this, but you probably get the idea. The user will see the
same question repeatedly until they give you the answer that you want,
and then you can move on to later code.

Also, it looks like there’s semicolons after your ‘if’ statements. I’m
not sure why they are there but it would be a good idea to get rid of
them.

much <3
joel

On Mon, Feb 14, 2011 at 7:47 PM, Anthony Ob [email protected]
wrote:

I want to know, what code i use, if someone doesnt enter the correct
word/phrase, lets say (using my script). I want to know how if they
enter something wrong like at the begining if they enter K_sdg_C, and
how do i put something like pick either bleh or bleh

You might look into the HighLine Gem:

http://highline.rubyforge.org/doc/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs