Simple Quiz Program

I want to create a simple Quiz program:
1- Display multiple-choice questions one by one (just three in my simple
case)
2- Answer each question with your choice letter
3- Display the number of successful answers
4- Display a good-bye message before exiting the program

Unfortunately, my program doesn’t get off the ground – I get the
following error:

/Users/marcc/.rvm/rubies/ruby-2.1.0/bin/ruby -e
$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
/Users/marcc/RubymineProjects/Tests/quiz.rb

/Users/marcc/RubymineProjects/Tests/quiz.rb:7:in display_question': wrong number of arguments (6 for 4) (ArgumentError) from /Users/marcc/RubymineProjects/Tests/quiz.rb:35:in<top
(required)>’
from -e:1:in load' from -e:1:in

In the above, what does "wrong number of arguments (6 for 4) mean?

Here is my program:

class Quiz

def initialize(correct_answers)
@correct_answers = correct_answers
end

def display_question( q1, q2, q3, answer)
loop do
puts q1
puts q2
puts q3
print “\nType a letter a, b, c, or d for your answer:”
reply = gets
if answer == reply then
@correct_answers += 1
end
end
end

def quiz_result
print ‘You correctly answered ’ + @correct_answers.to_s + ’
question(s).’
end

def end_game
puts “\t\tThanks for playing, see you next time!.”
end
end

my_quiz = Quiz.new(@correct_answers)

@correct_answers = 0

q1

puts ‘’
my_quiz.display_question(“How many places are called Paris in the
US?”,
“a. 17”, “b. 6”, “c. 25”, “d. 12”, “a”)

q2

puts ‘’
my_quiz.display_question(“What is Uzbekistan’s capital city?”,
“a. Qarshi”, “b. Tashkent”, “c. Bukhara”, “d.
Nukus”, “b”)

q3

puts ‘’
my_quiz.display_question(“What is the most spoken language in Afica?”,
“a. Swahili”, “b. French”, “c. Arabic”, “d.
English”, “d”)

display result

puts ‘’
my_quiz.quiz_result

display end game message

puts ‘’
my_quiz.end_game

The way you’ve built it you can only ever have 3 answers.
There are many ways to do this, but one simple option would be the pass
all your answer options as an Array:

def display_question( question, options, answer )
puts question
options.each_with_index { |option, idx| puts “#{ idx + 1 }: #{ option
}” }
print 'Answer: ’
reply = gets.to_i
if answer == reply
puts ‘Correct!’
@correct_answers += 1
else
puts 'Wrong. The correct answer was: ’ + answer.to_s
end
end

display_question ‘The answer is 3’, [ ‘Wrong’, ‘Wrong’, ‘Right!’,
‘Wrong’ ], 3

The answer is 3
1: Wrong
2: Wrong
3: Right!
4: Wrong
Answer: 3
Correct!

The answer is 3
1: Wrong
2: Wrong
3: Right!
4: Wrong
Answer: 4
Wrong. The correct answer was: 3

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs