<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16705" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2><FONT size=1>
<P>hi,</P>
<P>Can someone help me i would like to run this program 3 times or
more and would like to append the cPickle file as a high score table
keeping my top scores. Right now it only records the last score thanks.
</P></FONT></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2># Trivia Challenge<BR># Trivia game that reads a
plain text file</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>def open_file(file_name,
mode):<BR> """Open a file."""<BR>
try:<BR> the_file = open(file_name,
mode)<BR> except(IOError),
e:<BR> print "Unable to open the
file", file_name, "Ending program.\n",
e<BR> raw_input("\n\nPress the enter
key to exit.")<BR>
sys.exit()<BR>
else:<BR> return the_file</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>def next_line(the_file):<BR>
"""Return next line from the trivia file, formatted."""<BR>
line = the_file.readline()<BR> line = line.replace("/",
"\n")<BR> return line</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>def next_block(the_file):<BR>
"""Return the next block of data from the trivia file."""<BR>
category = next_line(the_file)<BR> <BR>
question = next_line(the_file)<BR> <BR>
answers = []<BR> for i in
range(4):<BR>
answers.append(next_line(the_file))<BR>
<BR> correct = next_line(the_file)<BR> if
correct:<BR> correct =
correct[0]<BR> <BR>
explanation = next_line(the_file) </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> return category, question,
answers, correct, explanation</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>def welcome(title):<BR>
"""Welcome the player and get his/her name."""<BR> print
"\t\tWelcome to Trivia Challenge!\n"<BR> print "\t\t", title,
"\n"<BR> <BR>def main():<BR> trivia_file =
open_file("trivia.txt", "r")<BR> title =
next_line(trivia_file)<BR>
welcome(title)<BR> score = 0<BR> bonus =
0<BR> tries = 0</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> # get first
block<BR> category, question, answers, correct, explanation =
next_block(trivia_file)<BR> while
category:<BR> # ask a
question<BR> print
category<BR> print
question<BR> for i in
range(4):<BR>
print "\t", i + 1, "-",
answers[i]<BR> <BR>
</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> # get
answer<BR> answer = raw_input("What's
your answer?: ")<BR> tries = tries +
1<BR> # check
answer<BR> if answer ==
correct:<BR>
print
"\nRight!",<BR>
score += 1<BR>
if tries ==
1:<BR>
bonus = 5<BR>
elif tries ==
2:<BR>
bonus = bonus +
10<BR> elif
tries ==
3:<BR>
bonus = bonus +
20<BR> elif
tries ==
4:<BR>
bonus = bonus +
30<BR> elif
tries ==
5:<BR>
bonus = bonus +
40<BR>
<BR>
<BR>
else:<BR>
bonus =
bonus<BR>
print "\nWrong.",<BR> print
explanation<BR> print "Score:", score,
"\n\n"<BR> </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> # get
next block<BR> category, question,
answers, correct, explanation = next_block(trivia_file)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> trivia_file.close()</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> print "That was the last
question!"<BR> print "You're score is:", score, "and your
bonus", bonus<BR> total = score + bonus<BR>
print "for a grand total ", total<BR> import cPickle,
shelve<BR> name = raw_input("what is your
name")<BR> High_Score = [name, total]<BR>
pickle_file = open("pickles5.dat", "w")<BR>
cPickle.dump(High_Score, pickle_file)<BR>
pickle_file.close()<BR> # to read the
pickle_file<BR> pickle_file = open("pickles5.dat",
"r")<BR> High_Score =
cPickle.load(pickle_file)<BR> print High_Score, "High
Scores"<BR>main() <BR>raw_input("\n\nPress the enter key to
exit.")<BR></FONT></DIV></BODY></HTML>