On Fri, 27 Jun 2003 01:44:42 GMT, Mike wrote:[color=blue]
> How do I extract a list of lists from a user defined function[/color]
Depends how that function is returning its values. The most obvious,
and simplest way to do what you describe, is to have the function return
a list of lists, as its return value.
If you mean something else, you'll have to be more descriptive of what
you actually want to do.
[color=blue]
> and print the results as strings for each list?[/color]
The 'repr()' method of any object will return a printable string of the
object's representation.
Ok, I'm doin an exercise and I need to expand on test.py. I'm pretty much
done with the exercise except I need to print out the questions and answers
that are in the function.
here is the snippet:
def get_questions() :
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","4 2"],\
["What is a three letter word for mouse trap?","cat"]]
How do I get the lists from this?
oh here is the whole program;
## This program runs a test of knowledge
true = 1
false = 0
# First get the test questions
# Later this will be modified to use file io.
def get_questions() :
# notice how the data is stored as a list of lists
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","4 2"],\
["What is a three letter word for mouse trap?","cat"]]
# This will test a single question
# it takes a single question in
# it returns true if the user typed the correct answer, otherwise false
def check_question( question_and_an swer):
#extract the question and the answer from the list
question = question_and_an swer[0]
answer = question_and_an swer[1]
# give the question to the user
given_answer = raw_input(quest ion)
# compare the user's answer to the testers answer
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct was:",answer
return false
# This will run through all the questions
def run_test(questi ons):
if len(questions) == 0:
print "No questions were given."
# the return exits the function
return
index = 0
right = 0
while index < len(questions):
#Check the question
if check_question( questions[index]):
right = right + 1
#go to the next question
index = index + 1
#notice the order of the computation, first multiply, then divide
print "You got ",right*100/len(questions), "% right out
of",len(questio ns)
#now lets run the questions
run_test(get_qu estions())
## This program runs a test of knowledge
true = 1
false = 0
# First get the test questions
# Later this will be modified to use file io.
def get_questions() :
# notice how the data is stored as a list of lists
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","4 2"],\
["What is a three letter word for mouse trap?","cat"]]
# This will test a single question
# it takes a single question in
# it returns true if the user typed the correct answer, otherwise false
def check_question( question_and_an swer):
#extract the question and the answer from the list
question = question_and_an swer[0]
answer = question_and_an swer[1]
# give the question to the user
given_answer = raw_input(quest ion)
# compare the user's answer to the testers answer
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct was:",answer
return false
# This will run through all the questions
def run_test(questi ons):
if len(questions) == 0:
print "No questions were given."
# the return exits the function
return
index = 0
right = 0
while index < len(questions):
#Check the question
if check_question( questions[index]):
right = right + 1
#go to the next question
index = index + 1
#notice the order of the computation, first multiply, then divide
print "You got ",right*100/len(questions), "% right out
of",len(questio ns)
#now lets run the questions
run_test(get_qu estions())
On Fri, 27 Jun 2003 03:15:56 GMT, Mike wrote:[color=blue]
> def get_questions() :
> return [["What color is the daytime sky on a clear day?","blue"],\
> ["What is the answer to life, the universe and
> everything?","4 2"],\
> ["What is a three letter word for mouse trap?","cat"]][/color]
These should not be a list of lists. To refer to a previous c.l.python
discussion, "for homogeneous data, use a list; for heterogeneous data,
use a tuple".
Thus, each question-and-answer pair is heterogeneous: it matters which
is which, and which position each is in; and extending it with more
items doesn't have any meaning.
On the other hand, a list of question-and-answer pairs is homogeneous:
each can be treated like any other question-and-answer pair, and the
list of them could be indefinitely extended or contracted without
distorting the meaning of the list.
So, get_questions() is better done with:
def get_questions() :
return [
( "What colour is a clear daytime sky?", "blue" ),
( "What is the answer to the ultimate question?", "42" ),
( "What is a three-letter word for mouse trap?", "cat" ),
]
(Note that placing a comma even after the last item in a list, allows
you to extend the list in the code without having a missing comma by
accident.)
Then, you iterate over the list of question-and-answer pairs, and get a
tuple of (question, answer) each time:
[color=blue][color=green][color=darkred]
>>> for (question, answer) in get_questions() :[/color][/color][/color]
print question, answer
What colour is a clear daytime sky? blue
What is the answer to the ultimate question? 42
What is a three-letter word for mouse trap? cat
On 27 Jun 2003 13:04:53 +0950, Ben Finney wrote:[color=blue][color=green][color=darkred]
> >>> for (question, answer) in get_questions() :[/color][/color]
> print question, answer[/color]
Dang, messed up the indentation when I pasted. This has the correct
indentation:
[color=blue][color=green][color=darkred]
>>> for (question, answer) in get_questions() :[/color][/color][/color]
print question, answer
What colour is a clear daytime sky? blue
What is the answer to the ultimate question? 42
What is a three-letter word for mouse trap? cat
Comment