[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> </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. 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> </DIV>
<DIV><FONT face=3DArial size=3D2> <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 < 3:<BR> print=20
'---------------------------'<BR> print '1. Take the=20
test?'<BR> print '2. View the questions, and the=20
answer.'<BR> print '3. Quit the program.=20
'<BR> menu_item =3D input('Pick an item from the menu: =
')<BR> if menu_item =3D=3D=20
1:<BR> current =3D=20
0<BR> if len(list) >=20
0:<BR> =
while=20
current <=20
len(list):<BR>  =
; =20
print current,'.=20
',list[current]<BR> =
=20
current =3D current +=20
1<BR> &n=
bsp; =20
print "Ok here we=20
go"<BR> =
=20
break<BR> elif menu_item =3D=3D=20
2:<BR> print=20
get_questions()<BR> =20
break<BR> =
else:<BR> =20
menu_item =3D=3D 3<BR> print =
"good=20
bye"<BR> =20
break<BR> &nbs=
p; =20
<BR># First get the test questions<BR># Later this will be modified to =
use file=20
io.<BR>def j_questions():<BR> return [["What color is =
the=20
daytime sky on a clear=20
day?"],\<BR> &=
nbsp;=20
["What is the answer to life, the universe and=20
everything\n"],\<BR>  =
; =20
["What is a three letter word for mouse trap?"]]</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>def =
get_questions():<BR> if=20
menu_item =3D=3D 1 or 2:<BR> =
#notice how=20
the data is stored as a list of=20
lists<BR> return [["What color =
is the=20
daytime sky on a clear day?",=20
"blue"],\<BR> =
=20
["What is the answer to life, the universe and=20
everything?","42"],\<BR> &=
nbsp; =20
["What is a three letter word for mouse trap?", =
"cat"]]<BR> =20
else:<BR> =20
return<BR> <BR> </FONT></DIV>
<DIV> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>def=20
check_questions(question_and_answer):<BR> #extract the =
question and the answer from the list<BR> question =3D =
question_and_answer[0]<BR> answer =3D=20
question_and_answer[1]<BR> #give the question to the=20
user<BR> given_answer =3D=20
raw_input(question)<BR> #compare the user's answer to =
the=20
testers answer<BR> if answer =3D=3D=20
given_answer:<BR> print=20
"Correct!!"<BR> return=20
true<BR> =
else:<BR> =20
print "Incorrect, correct=20
was:",answer<BR> return=20
false<BR> # This will run through all the=20
questions</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>def =
run_test(questions):<BR> if=20
len(questions) =3D=3D 0:<BR> =
print "No=20
questions were given."<BR> # =
the=20
return exits the function<BR> =20
return<BR> index =3D 0<BR> right =3D =
0<BR> while index <=20
len(questions):<BR> #Check the =
question<BR> if=20
check_questions(questions[index]):<BR>  =
; =20
right =3D right + 1<BR> #Go to =
the next=20
question<BR> &=
nbsp;=20
index =3D index + 1<BR> =
#Notice the=20
order of the computation, first multiply, the=20
divide<BR> print "You=20
got",right*100/len(questions),"% right out =
of",len(questions)</FONT></DIV>
<DIV> </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--