Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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 :)