1. To add this functionality, you will need to:
a. Create a new file trivia_points.txt. The format of this file will be the same as that for trivia.txt except that you need to add a number (the point value) to each question. You may do this any way you like, but probably the easiest is to put it on its own line, probably right after the explanation line.
b. Modify your code so that:
i. It reads the file trivia_points.txt rather than trivia.txt.
ii. Each time it reads a question it reads in the point value and stores it (in a variable). Important: exactly how you do this depends on the decision you made above in part a. Also, remember: when you read in a number, it is read as a string. You will need to be able to use it as a number (so you can add it to the score). The following code converts a string in the variable y to a number:
y = int(y)
iii. Each time a question is answered correctly, the score should be incremented by the given point value.
I don't really understand B.ii very well and how to get the code to read the point values. Help on that would be appreciated.
Here is the code to the main program the rest is all in a .txt file the program reads.
def main():
trivia_file = open_file("trivia.txt", "r")
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 = input("What's your answer?: ")
# check answer
if answer == correct:
print("\nRight!", end=" ")
score += 1
else:
print("\nWrong.", end=" ")
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)

New Topic/Question
Reply




MultiQuote








|