Gets.chomp input comparison returning an error

162 Views Asked by At

I AM USING RUBY

ERROR: comparison of Integer with String failed (ArgumentError)

puts "Age: "
    age = gets.chomp
    if 0 < age < 130

I want the programm to allow the user to input all the numbers between 0 (not included) and 130 (included). How to do it?

1

There are 1 best solutions below

7
On BEST ANSWER

The input is a string. Try something like this

puts "Age: "
user_input = gets.chomp
begin
  age = Integer(user_input)
  # your code
rescue ArgumentError
  puts "Age must be an integer"
end