Thursday, 22 August 2013

Ruby - undefined method `/' for "90 kilograms":String (NoMethodError)

Ruby - undefined method `/' for "90 kilograms":String (NoMethodError)

I tried to make a simple program that asks the user a few questions.
I wanted the program to assume the user was from America, but to convert
the measures if the user gave answers in the metric system. Here is the
code:
print "How old are you?"
age = gets.chomp()
print "Ok, how tall are you?"
height = gets.chomp()
if height.include? "centimeters"
height = height * 2.54
else
height = height
print "How much do you weigh?"
weight = gets.chomp()
if weight.include? "kilograms"
weight = weight / 2.2
else
weight = weight
puts "So, you're #{age} years old, #{height} tall and #{weight} pounds
heavy."
I ran the code and it was fine. I was able to enter in all my data until I
got to the last bit about the weight. I tested it by entering in 90
kilograms, but got the following error message:
ex11.rb:15:in `<main>': undefined method `/' for "90 kilograms":String
(NoMethodError)
It looks like it's not accepting my division operator. Does anyone know
what's up here?

No comments:

Post a Comment