+ 1

I want to weite a program that would require an input of age and depends what age write a certain message

Age = gets.chomp If age >= 18 Puts "Welcome" Else Puts "Too young" End What am I doing worong and how can i make it work? Thank you

20th Apr 2017, 3:39 PM
Yana Andreeva
Yana Andreeva - avatar
3 Réponses
+ 8
First problem: ruby is case sensitive, so it wont work with Age instead age, If instead if etc. Second problem: user input is a string, which cant be compared to a number. Lets say you enter "foo" how would you compare that to a number? you cant say that "foo" is greater or smaller than 18. If you enter a number, it is still interpreted as a string, e.g. if I enter "21" its still a string, not a number, and therefore cant be compared to a number. you have to turn it into a number using the to_i method. example: x = "42" y = x.to_i puts x * 2 # puts 4242 puts y * 2 # puts 84 Refer to @MrCoder to see the correct code.
20th Apr 2017, 8:38 PM
Supersebi3
Supersebi3 - avatar
+ 6
Your whole code should be: age = gets.chomp.to_i if age >= 18 puts "Welcome" else puts "Too young" end
20th Apr 2017, 3:53 PM
MrCoder
MrCoder - avatar
+ 5
I am NOT a ruby learner,But I think this would solve the problem:- 1. age =gets.chomp small a of age 2. convert age to integer, its in String
20th Apr 2017, 3:44 PM
Meharban Singh
Meharban Singh - avatar