Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Issue with my ruby code

Hey Everyone,

I'm trying to create a simple ruby program that asks 5 questions and displays the score. following is the code

def score(a, b, c, d, e)
    puts "#{a},#{b},#{c},#{d},#{e}"
  if a == 'D'
    puts "Right Answer"
    s = 1
  else
    puts "Wrong Answer"
  end
  if b == 'W'
    puts "Right Answer"
    s2 = s+1
  else
        s2 = s
    puts "Wrong Answer"
  end
  if c == 'B'
    puts "Right Answer"
    s3 = s2+1
  else
        s3 = s2
    puts "Wrong Answer"
  end
  if d == 'P'
    puts "Right Answer"
    s4 = s3+1
  else
        s4 = s3
    puts "Wrong Answer"
  end
  if e == 'L'
    puts "Right Answer"
    s5 = s4+1
  else
        s5 = s4
    puts "Wrong Answer"
  end
    puts "your score is #{s5}"
end

puts "capital of india?"
A1 = gets
puts "capital of USA?"
A2 = gets
puts "capital of china?"
A3 = gets
puts "capital of france?"
A4 = gets
puts "capital of UK?"
A5 = gets

score(A1, A2, A3, A4, A5)

The issue here is that it always displays wrong answer and empty score even you if you input the right answers into the gets prompt. So just curious to know what is going wrong here ?

1 Answer

Solved! i just had to change the gets prompt to gets.chomp :)