I am trying to modify this trivia program to read scores from a separate text file and then add the scores to a running total if the user answers correctly. Here is my code so far:
# Trivia Challenge
# Trivia game that reads a plain text file
import os
def open_file():
print "Which trivia episode would you like? It must have a .trv extension:"
print os.listdir(os.getcwd())
print "Type in the correct file name: "
file_name = raw_input()
"""Open a file."""
try:
the_file = open(file_name, 'r')
except(IOError), e:
print "Unable to open the file", file_name, "Ending program.\n", e
raw_input("\n\nPress the enter key to exit.")
sys.exit()
else:
return the_file
def next_line(the_file):
"""Return next line from the trivia file, formatted."""
line = the_file.readline()
line = line.replace("/", "\n")
return line
def next_block(the_file):
"""Return the next block of data from the trivia file."""
category = next_line(the_file)
question = next_line(the_file)
answers = []
for i in range(4):
answers.append(next_line(the_file))
correct = next_line(the_file)
if correct:
correct = correct[0]
explanation = next_line(the_file)
return category, question, answers, correct, explanation
def welcome(title):
"""Welcome the player and get his/her name."""
print "\t\tWelcome to Trivia Challenge!\n"
print "\t\t", title, "\n"
def main():
trivia_file = open_file()
title = next_line(trivia_file)
welcome(title)
score = 0
# get first block
category, question, answers, correct, explanation = next_block(trivia_file)
while category:
# ask a question
print category
print question
for i in range(4):
print "\t", i + 1, "-", answers[i]
# get answer
answer = raw_input("What's your answer?: ")
# check answer
if answer == correct:
print "\nRight!"
a = trivia_file.readline()
a = a(int)
score = score + a
else:
print "\nWrong.",
print explanation
print "Score:", score, "\n\n"
# get next block
category, question, answers, correct, explanation = next_block(trivia_file)
trivia_file.close()
print "That was the last question!"
print "You're final score is:", score
main()
raw_input("\n\nPress the enter key to exit.")
And here is my text file:
An Episode You Can't Refuse
On the Run With a Mammal
Let's say you turn state's evidence and need to "get on the lamb." If you wait /too long, what will happen?
You'll end up on the sheep
You'll end up on the cow
You'll end up on the goat
You'll end up on the emu
1
5
A lamb is just a young sheep
The Godfather Will Get Down With You Now
Let's say you have an audience with the Godfather of Soul. How would it be /smart to address him?
Mr. Richard
Mr. Domino
Mr. Brown
Mr. Checker
3
10
James Brown is the Godfather of Soul.
That's Gonna Cost Ya
If you paid the Mob protection money in rupees, what business would you most /likely be insuring?
Your tulip farm in Holland
Your curry powder factory in India
Your vodka distillery in Russian
Your army knife warehouse in Switzerland
2
15
The Rupee is the standard monetary unit of India.
Keeping It the Family
If your mother's father's sister's son was in "The Family," how are you related /to the mob?
By your first cousin once removed
By your first cousin twice removed
By your second cousin once removed
By your second cousin twice removed
1
20
Your mother's father's sister is her aunt -- and her son is your /mother's first cousin. Since you and your mother are exactly one generation /apart, her first cousin is your first cousin once removed.
A Maid Man
If you were to literally launder your money, but didn't want the green in your /bills to run, what temperature should you use?
Hot
Warm
Tepid
Cold
4
25
According to my detergent bottle, cold is best for colors that might run.
The score amount is listed after the correct answer and before an explanation of the answer.
The problem I'm running to is this part of my code:
# check answer
if answer == correct:
print "\nRight!"
a = trivia_file.readline()
a = a(int)
score = score + a
I keep getting an error telling me that TypeError: 'str' object is not callable. Which to me doesn't make sense because I thought that a was being converted to an integer before being added to score.
Any help would be much appreciated.
Thanks!