Search code examples
rubygeometryalgebrapythagorean

Why do I get get "Undefined local variable" in Pythagorean Theorem calculator?


I made a Pythagorean Theorem calculator in Ruby and it has a bug/does not run. The error is:

undefined local variable or method `a2'

I was wondering if anyone could help.

puts "Welcome to my pythagorean theorem calculator. This calculator can find the hypotenuse of any right triangle. Please enter side A of your triangle."
a = gets.to_f
puts "please enter side B of your triangle."
b = gets.to_f
a**2 == a2
b**2 == b2
a2 + b2 = a2_b2
puts "The hypotenuse of your triangle is: #{ Math.sqrt(a2_b2)}"

Solution

  • You just had two small errors:

    puts "Welcome to my pythagorean theorem calculator. This calculator can find the hypotenuse of any right triangle. Please enter side A of your triangle."
    a = gets.to_f
    puts "please enter side B of your triangle."
    b = gets.to_f
    a2 = a**2
    b2 = b**2
    a2_b2 = a2 + b2 
    puts "The hypotenuse of your triangle is: #{ Math.sqrt(a2_b2)}"
    

    --

    Welcome to my pythagorean theorem calculator. This calculator can find the hypotenuse of any right triangle. Please enter side A of your triangle.
    3
    please enter side B of your triangle.
    4
    The hypotenuse of your triangle is: 5.0