#Trivia Challenge

import sys

def 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 program.\n",e)
        input("\n \n Press 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("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 in range(4):
            print "\t",i+1,"-",answers[i]

        answer=input("What's your answers? :\n")
        if answer==correct:
            print("\nRight!")
            score=score+1
        else:
            print("\n Wrong")
        print(explanation)
        print "Score:",score,"\n\n"
        category,question,answers,correct,explanation=next_block(trivia_file)

    trivia_file.close()
    print("that's was the last question!")
    print("You're final score is",score)

main()
input("\n\n Press the enter key to exit")
