I want to do something like that.
puts "Please write your age: "
age = gets.chomp
if #{age}<18
puts "you are illegal"
else #{age}>18
puts "You are legal"
end
the output i get is:
"Please write your age" 15. you are illegal you are legal"
and this
"Please write your age 20 you are illegal you are legal"
Why? And what is the solution please?
What I expect is this If I write 19 or older, it will say "you are legal" And if I write 17 or any number below It will tell me "You are illegal"
#{}
is used for string interpolation, you don't need it there, and else statements don't work like this (elsif
does). You also need to convert the string to an integer. You could write it like this:
puts "Please write your age: "
age = gets.chomp.to_i
if age > 18 # Since you want 19 or older. You could use age > 17 or age >= 18 if you actually meant 18 or older.
puts "You are of legal age"
else
puts "You are not of legal age"
end
See an article explaining if and else in Ruby