[Tutor] Help with simple program using while loops and lists
sudhir chauhan
sudhirchauhan1@yahoo.co.in
Sat Apr 12 12:47:01 2003
Hi ,
how about this order of placement of your lines. i did
not modify anything except commenting one line. this
will make it work at least :)
regards,
sudhir
## This program runs a test of knowledge
# First get the test questions
# Later this will be modified to use file io.
def j_questions():
return [["What color is the daytime sky on a clear
day?"],\
["What is the answer to life, the universe
and everything\n"],\
["What is a three letter word for mouse
trap?"]]
def get_questions():
if menu_item == 1 or 2:
#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?","42"],\
["What is a three letter word for mouse
trap?", "cat"]]
else:
return
#This will test a single question
# it takes a single question in
#it returns true if the user typed the correct answer,
othrewise false
def check_questions(question_and_answer):
#extract the question and the answer from the list
question = question_and_answer[0]
answer = question_and_answer[1]
#give the question to the user
given_answer = raw_input(question)
#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(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
if check_questions(questions[index]):
right = right + 1
#Go to the next question
index = index + 1
#Notice the order of the computation, first
multiply, the divide
print "You got",right*100/len(questions),"%
right out of",len(questions)
#now lests run the questions
true = 1
false = 00
menu_item = 0
list = []
while menu_item < 3:
print '---------------------------'
print '1. Take the test?'
print '2. View the questions, and the answer.'
print '3. Quit the program. '
menu_item = input('Pick an item from the menu: ')
if menu_item == 1:
current = 0
if len(list) > 0:
while current < len(list):
print current,'. ',list[current]
current = current + 1
print "Ok here we go"
break
elif menu_item == 2:
run_test(get_questions())
break
else:
menu_item == 3
print "good bye"
break
________________________________________________________________________
Missed your favourite TV serial last night? Try the new, Yahoo! TV.
visit http://in.tv.yahoo.com