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"
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
+ 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.
0
I run it and in output it writes what's your age
And what does to_i means?
0
age=input("your age")
0
What is the output of this code?
x = 5
unless x < 8
x += 3
else
x += 2
end
puts x