Problem with exercise 14, Control Flow in Ruby


#1



When you run the code it asks you "What is your favorite number" and lets you write in any number of your choice, then says "My favorite number is 6. " like it should, but after that it displays the error message "comparison of String with 6 failed" instead of showing one of the 4 different options depending on what number you put in. What am I doing wrong?


print "What is your favorite number?"
your_number=gets
print "My favorite number is 6. "
my_number = 6

if your_number==my_number
  print "We have the same favorite number!"
elsif your_number>my_number
  print "Your favorite number is bigger than mine!"
else your_number<my_number
  print "Your favorite number is smaller than mine!"
end


#2

All you need to do is specify the data type and write gets.chomp and not just gets,

your_number=Integer(gets.chomp)

The rest of your code should work as intended. :slight_smile:


#3

Thanks, it works now!


#4

You're welcome :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.