[Tutor] Help with simple program using while loops and lists

Danny wheelcrdan@hotmail.com
Fri Apr 11 19:24:01 2003


This is a multi-part message in MIME format.

------=_NextPart_000_0005_01C3004E.F39C1F10
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Everyone,

Thanks ahead of time for everyone help. This program seems like it =
should be easy. All I want it to do is give the user a menu with three =
options. That seems easy but with the second and the third option does =
not do what I would except. I want the program to if they pick option 2 =
is print the questions and the answers, and print them one question and =
one answer on each line. Then break out of the loop and exit. The third =
option if they pick that all I want it to do is just break out of the =
loop and exit the program. Sounds easy but I been working all day and =
can't get it to do those simple tasks. Here is that program.

   =20
## This program runs a test of knowledge
true =3D 1
false =3D 00
menu_item =3D 0
list =3D []
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 =3D input('Pick an item from the menu: ')
    if menu_item =3D=3D 1:
        current =3D 0
        if len(list) > 0:
            while current < len(list):
                print current,'. ',list[current]
                current =3D current + 1
                print "Ok here we go"
                break
    elif menu_item =3D=3D 2:
        print get_questions()
        break
    else:
        menu_item =3D=3D 3
        print "good bye"
        break
               =20
# 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 =3D=3D 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
   =20
   =20

#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 =3D question_and_answer[0]
    answer =3D question_and_answer[1]
    #give the question to the user
    given_answer =3D raw_input(question)
    #compare the user's answer to the testers answer
    if answer =3D=3D 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) =3D=3D 0:
        print "No questions were given."
        # the return exits the function
        return
    index =3D 0
    right =3D 0
    while index < len(questions):
        #Check the question
        if check_questions(questions[index]):
            right =3D right + 1
        #Go to the next question
            index =3D 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
run_test(get_questions())

Thanks again and talk to you all again soon=20
Danny D
------=_NextPart_000_0005_01C3004E.F39C1F10
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1141" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi Everyone,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks ahead of time for everyone help. =
This=20
program seems like it should be easy. All I want it to do is give the =
user a=20
menu with three options. That seems easy but with the second and the =
third=20
option does not do what I would except. I want the program to if they =
pick=20
option 2 is print the questions and the answers, and print them one =
question and=20
one answer on each line.&nbsp;Then break out of the loop and exit. The =
third=20
option if they pick that all I want it to do is just break out of the =
loop and=20
exit the program. Sounds easy but I been working all day and can't get =
it to do=20
those simple tasks. Here is that program.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; <BR>## This program =
runs a test=20
of knowledge<BR>true =3D 1<BR>false =3D 00<BR>menu_item =3D 0<BR>list =
=3D []<BR>while=20
menu_item &lt; 3:<BR>&nbsp;&nbsp;&nbsp; print=20
'---------------------------'<BR>&nbsp;&nbsp;&nbsp; print '1. Take the=20
test?'<BR>&nbsp;&nbsp;&nbsp; print '2. View the questions, and the=20
answer.'<BR>&nbsp;&nbsp;&nbsp; print '3. Quit the program.=20
'<BR>&nbsp;&nbsp;&nbsp; menu_item =3D input('Pick an item from the menu: =

')<BR>&nbsp;&nbsp;&nbsp; if menu_item =3D=3D=20
1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; current =3D=20
0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if len(list) &gt;=20
0:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
while=20
current &lt;=20
len(list):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
print current,'.=20
',list[current]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
current =3D current +=20
1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;=20
print "Ok here we=20
go"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;=20
break<BR>&nbsp;&nbsp;&nbsp; elif menu_item =3D=3D=20
2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print=20
get_questions()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
break<BR>&nbsp;&nbsp;&nbsp; =
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
menu_item =3D=3D 3<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print =
"good=20
bye"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
break<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR># First get the test questions<BR># Later this will be modified to =
use file=20
io.<BR>def j_questions():<BR>&nbsp;&nbsp;&nbsp; return [["What color is =
the=20
daytime sky on a clear=20
day?"],\<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;=20
["What is the answer to life, the universe and=20
everything\n"],\<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;=20
["What is a three letter word for mouse trap?"]]</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def =
get_questions():<BR>&nbsp;&nbsp;&nbsp; if=20
menu_item =3D=3D 1 or 2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
#notice how=20
the data is stored as a list of=20
lists<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return [["What color =
is the=20
daytime sky on a clear day?",=20
"blue"],\<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;=20
["What is the answer to life, the universe and=20
everything?","42"],\<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
["What is a three letter word for mouse trap?", =
"cat"]]<BR>&nbsp;&nbsp;&nbsp;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
return<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>#This will test a single question<BR># =
it takes a=20
single question in<BR>#it returns true if the user typed the correct =
answer,=20
othrewise false</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def=20
check_questions(question_and_answer):<BR>&nbsp;&nbsp;&nbsp; #extract the =

question and the answer from the list<BR>&nbsp;&nbsp;&nbsp; question =3D =

question_and_answer[0]<BR>&nbsp;&nbsp;&nbsp; answer =3D=20
question_and_answer[1]<BR>&nbsp;&nbsp;&nbsp; #give the question to the=20
user<BR>&nbsp;&nbsp;&nbsp; given_answer =3D=20
raw_input(question)<BR>&nbsp;&nbsp;&nbsp; #compare the user's answer to =
the=20
testers answer<BR>&nbsp;&nbsp;&nbsp; if answer =3D=3D=20
given_answer:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print=20
"Correct!!"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
true<BR>&nbsp;&nbsp;&nbsp; =
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
print "Incorrect, correct=20
was:",answer<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
false<BR>&nbsp;&nbsp;&nbsp; # This will run through all the=20
questions</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def =
run_test(questions):<BR>&nbsp;&nbsp;&nbsp; if=20
len(questions) =3D=3D 0:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
print "No=20
questions were given."<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # =
the=20
return exits the function<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
return<BR>&nbsp;&nbsp;&nbsp; index =3D 0<BR>&nbsp;&nbsp;&nbsp; right =3D =

0<BR>&nbsp;&nbsp;&nbsp; while index &lt;=20
len(questions):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Check the =

question<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
check_questions(questions[index]):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
right =3D right + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Go to =
the next=20
question<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;=20
index =3D index + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
#Notice the=20
order of the computation, first multiply, the=20
divide<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "You=20
got",right*100/len(questions),"% right out =
of",len(questions)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>#now lests run the=20
questions<BR>run_test(get_questions())<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Thanks again and talk to you all again =
soon=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Danny D</DIV></FONT></BODY></HTML>

------=_NextPart_000_0005_01C3004E.F39C1F10--