0

How can i get an input from user and then use it in my if statement

Like if i want to ask a user how old is he? He inputs 19 My if statement says if age>18 puts "welcome" Esle puts "too young"

19th Apr 2017, 2:21 PM
Yana Andreeva
Yana Andreeva - avatar
5 Answers
+ 3
puts 'what\'s your age' age = gets.chomp.to_i if age >= 18 puts 'welcome !' else puts 'You shouldn\'t be watching this' end
19th Apr 2017, 2:43 PM
CHMD
CHMD - avatar
+ 3
to_i is used to convert a string (which is the user's input in this case) to an integer. if you don't use it in this scenario you'll get an error. well in a regular console it would have pirnted "what's your age" first, then asked the user to enter his age, then displayed the answer ( welcome or too young). Sololearn s compiler works a bit differently​. If u don't want "what's your age", you could simply delete the first line.
19th Apr 2017, 3:01 PM
CHMD
CHMD - avatar
0
I run it and in output it writes what's your age And what does to_i means?
19th Apr 2017, 2:51 PM
Yana Andreeva
Yana Andreeva - avatar
0
age=input("your age")
20th Apr 2017, 3:47 AM
Jason
0
What is the output of this code? x = 5 unless x < 8 x += 3 else x += 2 end puts x
16th Jul 2017, 4:02 PM
Monika Kothari
Monika Kothari - avatar