[prog] I don't understand this program (-- python) how does this work??
Guru -
haunter03 at hotmail.com
Fri Dec 13 14:47:27 EST 2002
I'm really confused on how this program works
So I've pasted the entire program to the mailing list, (this may be a bad
idea....)
Do mailing lists accept attachements?
Sorry about the length of this email
Note: not all of the indentation will be correct...
This program is taken from easytut (Non programmers tutorial for python),
not all of the comments are mine. The program does work, I simply don't
understand sections of it...
## This program runs a test of knowledge...(not a very good one)
#also add a menu interface, a thing to add new questions and answers,
#change questions and answers etc. etc.
#this program is quite advanced (for me)
true = 1
false = 0
#first get the test questions
def get_questions():
#note how the data is stored as a list of lists
return [["What colour is the sky?","blue"],\
["What is the answer to life, the universe and everything?","42"],\
["What is a three letter word for mouse trap?","cat"],\
["What noise does a truly advanced machine make?","ping"]]
I understand the above section returns a list of questions and answers.
#This will test a single question
#it takes a single question in
#it returns true if the user typed the answer in the list, otherwise false
def check_question(question_and_answer):
#extract the question and answer from the list
#how is this going to work??
I don't understand this section at all....I don't know how it works
question = question_and_answer[0]
answer = question_and_answer[1]
#give the question to the user
given_answer = raw_input(question)
#compare the users answer to the testers answer
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct answer was",answer
return flase
#This will run through all the questions
def run_test(questions):
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
#note check_questions includes giving the question
if check_question(questions[index]):
right = right + 1
I don't understandthe above two lines, how does the
check_question(questions[index): work? I know it runs check_questions(with a
value from the list questions?)
#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(questions)
#now lets run the questions
#get_questions is the list with the questions
run_test(get_questions())
I understand parts of the program but not others. Can anyone explain how the
sections I don't understand work?
_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
More information about the Programming
mailing list