Re: Simple Quiz Game
Marc Chanliau <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAAWV+PiAMm6Otb4UmPrLoGfhY6Q2P5xS9DWOrExn0_f72dVV0Q@mail.gmail.com> |
Jerry, I followed your advice. Breaking down the program into several
classes is more manageable. I've changed a few things in your program, most
notably the fact that I print the results from the take_quiz method (seems
more logical to me) and I've modified its format. Here is the program now
(works fine):
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 "You have answered #{@nb_of_correct_answers} questions correctly"
}
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
Now, I have two things to do:
(1) Handle incorrect user input
(2) Feed the questions from a separate text file
Thanks for the help. I got me out of the rut.
On Tue, Dec 15, 2015 at 2:16 PM, Jerry Davis <[email protected]> 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 Thompson
>
> On Tue, Dec 15, 2015 at 2:55 PM, Marc Chanliau <[email protected]>
> 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 <[email protected]> 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
>>>> <[email protected] <mailto:[email protected]>> 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
>>>> <[email protected] <mailto:[email protected]>> 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
>>>> <[email protected] <mailto:[email protected]>>
>>>> > 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
>>>> <[email protected] <mailto:[email protected]>>
>>>> >> 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 <
>>>> [email protected]
>>>> <mailto:[email protected]>> 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 <[email protected]
>>>> <mailto:[email protected]>> 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
>>>> <[email protected] <mailto:[email protected]>>
>>>> >>>>> 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
>>>> <[email protected] <mailto:[email protected]>> 写道:
>>>>
>>>> >>>>>>
>>>> >>>>>> 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.)
>>>
>>
>>
>
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>