import sys
defopen_file(file_name,mode):"""Open a file."""try:
the_file =open(file_name,mode)except IOError as e:print("Unable to open the file",file_name,"Ending program.\n",e)input("\n\nPress the enter key to exit.")
sys.exit()else:return the_file
defnext_line(the_file):"""Return next line from the trivia file,formatted"""
line = the_file.readline()
line = line.replace("/","\n")return line
defnext_block(the_file):"""Return the next block of data from the trivia file."""
category = next_line(the_file)
question = next_line(the_file)
answer =[]for i inrange(4):
answer.append(next_line(the_file))
correct = next_line(the_file)if correct:
correct = correct[0]
explanation = next_line(the_file)return category,question,answer,correct,explanation
defwelcome(title):"""Welcome the player and get his/her name."""print("\t\tWelcome to trivia Challenge!\n")print("\t\t",title,"\n")defmain():
trivia_file = open_file("trivia.txt","r")
title = next_line(trivia_file)
welcome(title)
score =0
category,question,answers,correct,explanation = next_block(trivia_file)while category:print(category)print(question)for i inrange(4):print("\t",i+1,"-",answers[i])
answer =input("What's your answer?:")if answer == correct:print("\nRight!",end=" ")
score +=1else:print("\nWrong.",end=" ")print(explanation)print("Score:",score,"\n\n")
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()
Welcome to trivia Challenge!
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?
1 - You'll end up on the sheep
2 - You'll end up on the cow
3 - You'll end up on the goat
4 - You'll end up on the emu
What's your answer?:1
Right! A lamb is just a young sheep
Score: 1
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 adress him?
1 - Mr.Richard
2 - Mr.Domino
3 - Mr.Brown
4 - Mr.Checker
What's your answer?:3
Right! James Brown is the Godfather of Soul.
Score: 2
That was the last question
You're final score is 2
import sysdef open_file(file_name,mode): """Open a file.""" try: the_file = open(file_name,mode) except IOError as e: print("Unable to open the file",file_name,"Ending pr...