This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by ©arlos Ⓜedina
about 10 years

Not sure what the error means when i type this code

print 'Hello age please:'
age = gets.chomp

unless age >= 18
    print 'You are years old; meaning you are old enough. Welcome!'
    
else age < 18
    print 'Sorry you are years old; better luck next year!'
    
end

Error is: comparison of String with 18 failed

Alex J edited this post to fix code formatting

Answer 52c55a929c4e9df34b0074f3

1 vote

Permalink

The error tells you that Ruby cannot compare a String (a sequence of text characters, in this case, whatever the user typed in, say, "21") to the number 18 because Strings and numbers are incompatible data types.

In order to compare age to 18, you need to convert age into a number first:

age = gets.chomp.to_i

(where to_i means “to integer“, which is essentially “to number”).

points
Submitted by Alex J
about 10 years

2 comments

Anya about 10 years

Thank you so much for explaining this, you helped with a problem I was having as well.

Alexander Michailov over 9 years

Thanks too