Ranking
Original Post
Ruby language questions
So, I was playing around with Ruby on nitrous.io. Started learning it a couple of weeks ago.
I made this "survey" in which I tested a few commands/statements, but I don't know how to include the total amount of correct/incorrect answers at the end of the survey.
This is the code:
Ruby code:
puts "Hello! Welcome to my survey!"
# FIRST QUESTION
puts "First question: What is my favorite color?"
answer1 = gets.chomp
if answer1 == "Black"
puts "Correct!"
elsif answer1 == "black"
puts "Correct!"
else
puts "Incorrect!"
end
# THIRD QUESTION
puts "Third question: What's my favorite soccer team?"
answer3 = gets.chomp
if answer3 == "Real Madrid"
puts "Correct!"
elsif answer3 == "Arsenal"
puts "Correct!"
elsif answer3 == "real madrid"
puts "Correct!"
elsif answer3 == "arsenal"
puts "Correct!"
else
puts "Incorrect!"
end
# FOURTH QUESTION
puts "Fourth question: Who is my favorite tennis player?"
answer4 = gets.chomp
if answer4 == "Djokovic"
puts "Correct!"
elsif answer4 == "djokovic"
puts "Correct!"
else
puts "Incorrect!"
end
# FIFTH QUESTION
puts "What's my username?"
answer5 = gets.chomp
if answer5 == "Nerti"
puts "Correct!"
elsif answer5 == "nerti"
puts "Correct!"
else
puts "Incorrect!"
end

I want it to reply something along the lines of "You got 2 correct and 2 incorrect!".
Anybody know how to do this?
Last edited by nerti; May 1, 2015 at 06:01 PM.
Modo Bestia
First you should declare a variable for the correct and the incorrect answers, you can do it like this:

code:
$correct_answers = 0
$incorrect_answers = 0

puts "Hello! Welcome to my survey!"

# FIRST QUESTION

puts "First question: What is my favorite color?"

answer1 = gets.chomp

if answer1 == "Black"

puts "Correct!"
$correct_answers += 1

elsif answer1 == "black"

puts "Correct!"
$correct_answers += 1

else

puts "Incorrect!"
$incorrect_answers += 1

end...


Then at the end of each answer, if that's right you add +1 to the $correct_answers value, if it's wrong... well, you got it.

PS: This is not the best way to handle it, but since you seem to be starting, go for it and play with this method for a while.

Edit: I forgot your final request, if you want to print the result just add this to your code

code:
puts "You have #$correct_answers answers right!"
Last edited by IIInsanEEE; May 1, 2015 at 06:35 PM.
Can I get PM'd the final code please? Also what does it run on? Do you need to download Ruby or something?
| Leader of FC | Loans | ABD Enthusiast |
Originally Posted by ZENBOY123 View Post
Can I get PM'd the final code please? Also what does it run on? Do you need to download Ruby or something?

1. I'll pm it to you when I can.
2. lite.Nitrous.io - search it, make a box and select a language. If you need some help with it, just ask.
3. As said above, use nitrous.
Modo Bestia
Thanks Nerti I like Coding in Multiple Languages and I also love Encrypting ^_^ Ur response was helpful
| Leader of FC | Loans | ABD Enthusiast |