This forum is now read-only. Please use our new forums! Go to forums
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
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”).
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
2 comments
Thank you so much for explaining this, you helped with a problem I was having as well.
Thanks too