Jerry, I've followed your advice. Having multiple smaller classes is easier
to understand what's going on. I took your suggestion, modified it slightly
(I put the counter of good answers in the take_quiz method so that the user
knows instantly if he answered the question correctly. This seems to be
working well. Now I'll work on two things:
1- Manage user input errors
2- Put all my trivia questions in a separate text file.
Thanks again for the answer, it was very helpful.
Here the program I have now:
class QuestionClass
attr_reader :correct_answer
def initialize(question, choices, correct_answer)
@question = question
@choices = choices
@correct_answer = correct_answer
end
def display
puts @question
a = Array('a'...'z')
i = 0
@choices.each { |c|
puts " #{a[i]}. #{c}"
i += 1
}
end
end
class QuizClass
attr_reader :nb_of_correct_answers
def initialize
@q = Array.new
@nb_of_correct_answers = 0
end
def add_question(q, choices, a)
question = QuestionClass.new(q, choices, a)
@q << question
end
def take_quiz
@q.each { |qq|
qq.display
print "\nanswer? "
a = gets
a = a.chomp
@nb_of_correct_answers += 1 if a == qq.correct_answer
puts "So far, you've answered #{@nb_of_correct_answers} question(s)
correctly"
puts '' # for clarity
}
end
end
quiz = QuizClass.new
quiz.add_question('How many places are called Paris in the US?', [17, 6,
25], 'a')
quiz.add_question("What is Uzbekistan's capital city?", ['Qarshi',
'Tashkent', 'Bukhara'], 'b')
quiz.add_question('What is the most spoken language in Africa?',
['Swahili', 'Arabic', 'English'], 'c')
quiz.take_quiz
···
On Tue, Dec 15, 2015 at 2:16 PM, Jerry Davis <jdawgaz@gmail.com> wrote:
marc,
look back to my response, and study it.
I believe you will find what you are looking for.jerry
--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer*The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov*I*
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous*If writing good code requires very little comments, then writing really
excellent code requires no comments at all!*- Ken ThompsonOn Tue, Dec 15, 2015 at 2:55 PM, Marc Chanliau <marc.chanliau@gmail.com> > wrote:
Thanks, Joe. Yes, ultimately, I will try to have the questions in a
separate text file, but I'm not there yet! The idea of having several
classes sounds interesting. I'll look into it.On Tue, Dec 15, 2015 at 1:04 PM, Joe Gain <joe.gain@gmail.com> wrote:
On 15.12.2015 19:48, Marc Chanliau wrote:
Jesus, thanks. I realize that I'm looping on one question only. I want
to loop over all the questions but the challenge is that I want to
collect the data (whether the user provided the right answers or not) to
display the result at the end. The key here is the "collect the data"
part that I'm struggling with.On Tue, Dec 15, 2015 at 12:41 AM, Jesús Gabriel y Galán >>>> <jgabrielygalan@gmail.com <mailto:jgabrielygalan@gmail.com>> wrote:
The problem is that you are looping inside the display_question
method, which, I guess, is supposed to handle just one question. You
need to remove the loop.Jesus.
On Mon, Dec 14, 2015 at 9:57 PM, Marc Chanliau >>>> <marc.chanliau@gmail.com <mailto:marc.chanliau@gmail.com>> wrote:
> This is what I have for now. It needs a lot of work! Bottom line
is I want
> to know how Ruby allows one to "buffer" user responses to
questions until
> later for displaying results.
> Here goes:
>
> class Quiz
>
> def initialize(correct_answers)
> @correct_answers = correct_answers
> end
>
> def display_question( question, a, b, c, d, answer)
> loop do
> puts question
> puts a
> puts b
> puts c
> puts d
> 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
>
>
> On Mon, Dec 14, 2015 at 12:33 PM, Mendel Schneerson >>>> <mmschneerson@gmail.com <mailto:mmschneerson@gmail.com>> >>>> > wrote:
>>
>> Hey Marc can you paste your new code after you edited it.
>>
>> mendel
>>
>> On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau >>>> <marc.chanliau@gmail.com <mailto:marc.chanliau@gmail.com>> >>>> >> wrote:
>>>
>>> Jerry, done that. I do get the first question, but I'm not
iterating
>>> through the other questions in my loop.
>>>
>>> On Mon, Dec 14, 2015 at 6:14 AM, Jerry Davis < >>>> jdawgaz@gmail.com >>>> <mailto:jdawgaz@gmail.com>> wrote:
>>>>
>>>> to put it as simply as I can:
>>>>
>>>> def display_question( q1, q2, q3, answer) has 4 arguments
>>>>
>>>> when you call it:
>>>>
>>>> my_quiz.display_question("How many places are called Paris in
the US?",
>>>> "a. 17", "b. 6", "c. 25", "d. 12", "a") has 6 arguments
>>>>
>>>> you must call it with the same number of arguments as you
defined. you
>>>> defined 4, and called it with 6.
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Extra Ham Operator: K7AZJ
>>>> Registered Linux User: 275424
>>>> Raspberry Pi and Arduino developer
>>>>
>>>> The most exciting phrase to hear in science - the one that
heralds new
>>>> discoveries - is not "Eureka!" but "That's funny...".
>>>> - Isaac. Asimov
>>>>
>>>> If you give someone a program, you will frustrate them for a
day; if you
>>>> teach them how to program, you will frustrate them for a
lifetime.
>>>> - Anonymous
>>>>
>>>> If writing good code requires very little comments, then
writing really
>>>> excellent code requires no comments at all!
>>>> - Ken Thompson
>>>>
>>>>
>>>> On Sun, Dec 13, 2015 at 10:09 PM, Kevin <kcclaas@gmail.com >>>> <mailto:kcclaas@gmail.com>> wrote:
>>>>>
>>>>> When you call a method, the arguments you pass into it become
local
>>>>> variables within the method. Typically this means you have to
pass the same
>>>>> number of arguments into the method as it asks for.
>>>>>
>>>>> When you write:
>>>>> my_quiz.display_question("How many places are called
Paris in the
>>>>> US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
>>>>>
>>>>> the method display_question sets:
>>>>> q1 = "How many places are called Paris in the US?"
>>>>> q2 = "a. 17"
>>>>> q3 = "b. 6"
>>>>> answer = "c. 25"
>>>>>
>>>>> but it has no idea what to do with "d. 12" or "a". Hence the
error. If
>>>>> you want to display the question, and always have a choice of
4 answers,
>>>>> then one way to go would be:
>>>>>
>>>>> def display_question(question, a, b, c, d, answer)
>>>>> puts question
>>>>> puts a
>>>>> puts b
>>>>> puts c
>>>>> puts d
>>>>> ...
>>>>>
>>>>> -Kevin
>>>>>
>>>>>
>>>>> On 12/13/2015 10:46 PM, Marc Chanliau wrote:
>>>>>
>>>>> Understood. I had noticed that before. However, what I don't
seem to
>>>>> understand is the relationship between what I include in my
questions (6
>>>>> arguments) and the arguments that are related to my method (4
arguments: 3
>>>>> questions and the answer for each).
>>>>>
>>>>>
>>>>> On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing >>>> <zhenpingfeng@gmail.com <mailto:zhenpingfeng@gmail.com>> >>>> >>>>> wrote:
>>>>>>
>>>>>> Check the line 7 and line 35, 38, 41. You will find the
answer. The
>>>>>> method only have 4 parameter.
>>>>>>
>>>>>> 在 2015年12月14日,上午9:49,Marc Chanliau
<marc.chanliau@gmail.com <mailto:marc.chanliau@gmail.com>> 写道:>>>>>>
>>>>>> 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 `<main>'
>>>>>>
>>>>>> 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
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>A user interface is usually such a pain in the arse to write that it's a
good idea to split it off into a module, or at least a class or two.A good place to start with a problem like this is to think about what
classes you need. While there are different ways of skinning this cat, you
might for example have:Questions: A question is associated to a correct answer.
Answers/Responses: An answer/response is a potential answer to a question
Rounds: A round consists of a question and n answers/responses (one of
the answers has to be correct) and return true or false depending on the
response entered.Score: Number of rounds correctly answered.
The UI has to print rounds, get user response and print the score. Don't
worry about having classes that don't seem to do much especially because
the ruby library seems so powerful.(Anyway, then you probably want to read in the questions and answers
from a text file.)