How to make this work , can someone sujjest ? other than the switch


#1

puts "choose a number"
number=gets.chomp

if number===1
puts"you win"
elsif number===2
puts "u were close"
else
puts "you lose"
end


#2

First of all, please add a space between operator and operands:
my_operand = my_operand

That is the standard coding style.

The second thing is, when you do gets.chomp, you are actually taking in a string, not an integer. So in your comparison, you are checking an integer against a string. In order for that to work, you could cast the input to an Integer, like this:

number = Integer(gets.chomp)

And just a note about triple equals: in Ruby, == means check for equal type AND equal value. This is in contrast with JavaScript, which uses the triple equal to check for both type and value! In Ruby, a triple === means you are just checking for a match! It could be a regular expression or an object type, for example.

With all that, use the following code (using === is also OK):

puts "choose a number"
number = Integer(gets.chomp)

if number == 1
  puts "you win"
elsif number == 2
  puts "u were close"
else
  puts "you lose"
end

#3

thanks for the explanation mate :smile: