Discussion:
Simple Quiz Game
Marc Chanliau
2015-12-14 01:49:14 UTC
Permalink
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
Feng ZhenPing
2015-12-14 02:15:22 UTC
Permalink
Check the line 7 and line 35, 38, 41. You will find the answer. The method only have 4 parameter.
Post by Marc Chanliau
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Jerry Davis
2015-12-14 04:43:36 UTC
Permalink
here is my take on what you are trying to do: (I am a learner too)

#!/usr/bin/env ruby


class Q

attr_reader :correct_answer


def initialize(q, choices, correct_answer)

@question = q

@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 Quiz

attr_reader :number_correct


def initialize

@q = Array.new

@number_correct = 0

end


def addquestion(q, choices, a)

question = Q.new(q, choices, a)

@q << question

end


def take_quiz

@q.each { |qq|

qq.display

print "\nanswer? "

a = gets

a = a.chomp

@number_correct += 1 if a == qq.correct_answer

puts ""

}

end

end


quiz = Quiz.new

quiz.addquestion("question #1", ['choice 1.1', 'choice 1.2', 'choice 1.3'],
'a')

quiz.addquestion("question #2", ['choice 2.1', 'choice 2.2', 'choice 2.3'],
'c')

quiz.take_quiz

puts quiz.number_correct


--
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
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The method
only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Marc Chanliau
2015-12-14 04:46:08 UTC
Permalink
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The method
only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Scott Leitch
2015-12-14 04:57:15 UTC
Permalink
A starting point could be to look into variable-length argument lists or
passing a hash to #display_question
Post by Marc Chanliau
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Kevin
2015-12-14 05:09:51 UTC
Permalink
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
Post by Marc Chanliau
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).
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
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Jerry Davis
2015-12-14 14:14:17 UTC
Permalink
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

*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
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Marc Chanliau
2015-12-14 20:03:11 UTC
Permalink
Jerry, done that. I do get the first question, but I'm not iterating
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
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
*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
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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,
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Mendel Schneerson
2015-12-14 20:33:27 UTC
Permalink
Hey Marc can you paste your new code after you edited it.

mendel
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not iterating
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
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
*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
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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,
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Marc Chanliau
2015-12-14 20:57:43 UTC
Permalink
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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not iterating
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
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
*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
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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,
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Jesús Gabriel y Galán
2015-12-15 08:41:13 UTC
Permalink
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.
Post by Marc Chanliau
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.
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
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not iterating
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
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
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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,
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
thomas Perkins
2015-12-15 14:11:02 UTC
Permalink
You know it's legit when Jesus answers your question

Sent from my iPhone
Post by Jesús Gabriel y Galán
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.
Post by Marc Chanliau
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.
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
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not iterating
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
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
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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,
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Marc Chanliau
2015-12-15 18:48:31 UTC
Permalink
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 <
Post by Jesús Gabriel y Galán
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.
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line is I
want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to questions until
later for displaying results.
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
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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 <
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not iterating
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in the
US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a lifetime.
- Anonymous
If writing good code requires very little comments, then writing
really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of 4
answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing <
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Joe Gain
2015-12-15 21:04:47 UTC
Permalink
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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?",
Post by Marc Chanliau
"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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a lifetime.
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it become
local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have to
pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of
4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
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?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris
in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# 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.)
Marc Chanliau
2015-12-15 21:55:50 UTC
Permalink
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.
Post by Joe Gain
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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.
Post by Marc Chanliau
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language in
Afica?",
Post by Marc Chanliau
"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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a
lifetime.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it become
local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have to
pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of
4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
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?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris
in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Qarshi", "b. Tashkent", "c. Bukhara", "d.
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language
in
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Afica?", "a. Swahili", "b. French", "c. Arabic",
"d.English", "d")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# 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
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.)
Jerry Davis
2015-12-15 22:16:50 UTC
Permalink
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
Post by Marc Chanliau
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.
Post by Joe Gain
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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.
Post by Marc Chanliau
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language in
Afica?",
Post by Marc Chanliau
"a. Swahili", "b. French", "c. Arabic",
"d.
Post by Marc Chanliau
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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a
lifetime.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it become
local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have to
pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of
4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
wrong number of arguments (6 for 4) (ArgumentError)
from /Users/marcc/RubymineProjects/Tests/quiz.rb:35:in
`<top
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
(required)>'
from -e:1:in `load'
from -e:1:in `<main>'
In the above, what does "wrong number of arguments (6 for 4)
mean?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris
in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Qarshi", "b. Tashkent", "c. Bukhara", "d.
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language
in
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Afica?", "a. Swahili", "b. French", "c. Arabic",
"d.English", "d")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# 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
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.)
Marc Chanliau
2015-12-16 00:12:46 UTC
Permalink
Jerry, I'm back to that. Trying to figure out the arrays, but it basically
does what I want it to do.
Post by Jerry Davis
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
Post by Marc Chanliau
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.
Post by Joe Gain
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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.
Post by Marc Chanliau
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language in
Afica?",
Post by Marc Chanliau
"a. Swahili", "b. French", "c. Arabic",
"d.
Post by Marc Chanliau
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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
On Mon, Dec 14, 2015 at 6:14 AM, Jerry Davis <
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a
lifetime.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it become
local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have to
pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of
4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
wrong number of arguments (6 for 4) (ArgumentError)
from /Users/marcc/RubymineProjects/Tests/quiz.rb:35:in
`<top
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
(required)>'
from -e:1:in `load'
from -e:1:in `<main>'
In the above, what does "wrong number of arguments (6 for 4)
mean?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris
in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Qarshi", "b. Tashkent", "c. Bukhara", "d.
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken
language in
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Afica?", "a. Swahili", "b. French", "c. Arabic",
"d.English", "d")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# 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
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.)
Marc Chanliau
2015-12-16 19:14:26 UTC
Permalink
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.
Post by Jerry Davis
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
Post by Marc Chanliau
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.
Post by Joe Gain
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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.
Post by Marc Chanliau
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language in
Afica?",
Post by Marc Chanliau
"a. Swahili", "b. French", "c. Arabic",
"d.
Post by Marc Chanliau
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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
On Mon, Dec 14, 2015 at 6:14 AM, Jerry Davis <
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a
lifetime.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it become
local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have to
pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of
4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
wrong number of arguments (6 for 4) (ArgumentError)
from /Users/marcc/RubymineProjects/Tests/quiz.rb:35:in
`<top
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
(required)>'
from -e:1:in `load'
from -e:1:in `<main>'
In the above, what does "wrong number of arguments (6 for 4)
mean?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris
in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Qarshi", "b. Tashkent", "c. Bukhara", "d.
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken
language in
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Afica?", "a. Swahili", "b. French", "c. Arabic",
"d.English", "d")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# 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
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.)
Marc Chanliau
2015-12-18 18:45:17 UTC
Permalink
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
Post by Jerry Davis
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
Post by Marc Chanliau
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.
Post by Joe Gain
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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.
Post by Marc Chanliau
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language in
Afica?",
Post by Marc Chanliau
"a. Swahili", "b. French", "c. Arabic",
"d.
Post by Marc Chanliau
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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
On Mon, Dec 14, 2015 at 6:14 AM, Jerry Davis <
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a
lifetime.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it become
local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have to
pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice of
4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method (4
arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
wrong number of arguments (6 for 4) (ArgumentError)
from /Users/marcc/RubymineProjects/Tests/quiz.rb:35:in
`<top
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
(required)>'
from -e:1:in `load'
from -e:1:in `<main>'
In the above, what does "wrong number of arguments (6 for 4)
mean?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris
in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Qarshi", "b. Tashkent", "c. Bukhara", "d.
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken
language in
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
Afica?", "a. Swahili", "b. French", "c. Arabic",
"d.English", "d")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# 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
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.)
Patrick Bayford
2015-12-15 21:54:21 UTC
Permalink
Post by Marc Chanliau
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
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
Post by Marc Chanliau
This is what I have for now. It needs a lot of work! Bottom line
is I want
Post by Marc Chanliau
to know how Ruby allows one to "buffer" user responses to
questions until
Post by Marc Chanliau
later for displaying results.
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
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
"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.
Post by Marc Chanliau
Nukus", "b")
# q3
puts ''
my_quiz.display_question("What is the most spoken language in
Afica?",
Post by Marc Chanliau
"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
Post by Mendel Schneerson
Hey Marc can you paste your new code after you edited it.
mendel
On Mon, Dec 14, 2015 at 3:03 PM, Marc Chanliau
Post by Marc Chanliau
Jerry, done that. I do get the first question, but I'm not
iterating
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
through the other questions in my loop.
On Mon, Dec 14, 2015 at 6:14 AM, Jerry Davis
Post by Jerry Davis
def display_question( q1, q2, q3, answer) has 4 arguments
my_quiz.display_question("How many places are called Paris in
the US?",
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
"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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
teach them how to program, you will frustrate them for a
lifetime.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
- Anonymous
If writing good code requires very little comments, then
writing really
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
excellent code requires no comments at all!
- Ken Thompson
Post by Kevin
When you call a method, the arguments you pass into it
become local
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
variables within the method. Typically this means you have
to pass the same
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
number of arguments into the method as it asks for.
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
you want to display the question, and always have a choice
of 4 answers,
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
Understood. I had noticed that before. However, what I don't
seem to
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
understand is the relationship between what I include in my
questions (6
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
arguments) and the arguments that are related to my method
(4 arguments: 3
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
questions and the answer for each).
On Sun, Dec 13, 2015 at 6:15 PM, Feng ZhenPing
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the
answer. The
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
method only have 4 parameter.
圚 2015幎12月14日䞊午9:49Marc Chanliau
1- Display multiple-choice questions one by one (just three
in my
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
/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
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?
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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 + '
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@correct_answers = 0
# q1
puts ''
my_quiz.display_question("How many places are called
Paris in the
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
US?","a. 17", "b. 6", "c. 25", "d. 12", "a")
# q2
puts ''
my_quiz.display_question("What is Uzbekistan's capital
city?", "a.
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
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")
Post by Marc Chanliau
Post by Mendel Schneerson
Post by Marc Chanliau
Post by Jerry Davis
Post by Kevin
Post by Feng ZhenPing
# display result
puts ''
my_quiz.quiz_result
# display end game message
puts ''
my_quiz.end_game
No virus found in this message.
Checked by AVG - www.avg.com <http://www.avg.com>
Version: 2016.0.7294 / Virus Database: 4489/11185 - Release Date: 12/15/15
Marc, you need to set up a place to store the answers and/or
correct/wrong, and store the result BEFORE exiting you quesrion loop.
--
Patrick Bayford Tel : 020 8265 8376 E-mail : ***@talktalk.net
Marc Chanliau
2015-12-14 20:02:19 UTC
Permalink
Kevin, that helps. At least the question gets displayed and the user can
type his answer. However, after entering the answer to the first question,
the same question gets displayed again. In other words, I'm not iterating
through the questions. That was the purpose of my exercise: Iterate through
questions, gather responses, and display result.
Post by Kevin
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.
my_quiz.display_question("How many places are called Paris in the
US?", "a. 17", "b. 6", "c. 25", "d. 12", "a")
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
def display_question(question, a, b, c, d, answer)
puts question
puts a
puts b
puts c
puts d
...
-Kevin
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).
Post by Feng ZhenPing
Check the line 7 and line 35, 38, 41. You will find the answer. The
method only have 4 parameter.
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
/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
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?
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
question(s).'
end
def end_game
puts "\t\tThanks for playing, see you next time!."
end
end
@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
Loading...