#Activity 7.2.1 - quiz version 2

import random

#List of questions and answers - correct answers start with C/; incorrect answers with I/
quizQuestions = [["What is CEOP?","C/Child Exploitation and Online Protection", "I/Criminal Exploration and Online Protection", "I/Child Exploitation and Organised Protectors"],
        ["When you get an email from someone you do not know what should you do?", "C/Delete it and mark as spam.", "I/Reply and say hello.", "I/Forward to your friends."],
        ["How secret should you keep your passwords?", "C/Never give out passwords except to your parents.", "I/Give them only to your best friends.", "I/Give them to strangers."],
        ["When an online contact who frightens you asks to meet you in person what should you do?", "C/Report to CEOP.", "I/Arrange to meet them.", "I/Arrange to meet them with your best friend.", ],
        ["If an email asks you to enter your bank account details because of a problem with your account what should you do?", "C/Contact the bank to check if they sent the email.", "I/Reply to the email.", "I/Enter your bank account details."],
        ["If someone you don't know asks you to upload a photo of yourself what should you do?", "I/Find a good photo of yourself and upload it.", "C/Tell your parents straight away.", "I/Tell all your friends that you have an unknown admirer."],
        ["If someone in a chat session uses sexually explicit language what should you do?", "I/Ignore it and continue chatting.", "I/Reply using similar language.", "C/Leave the chat session and remove that person from your friends list."]]

pointsPerQuestion = 4
availableQuestions = len(quizQuestions) - 1
#Using variables to store length of quiz and points per question allows the number of questions in the quiz and the number of points per question to be altered easily

def new_player():
    name = input ("\nPlease enter your name: ")
    print("\nHello",name,"- welcome to the quiz!\n")
    return name

def how_many_questions():
    print("You can choose the number of questions you are asked. The minimum is 4. The maximum is", len(quizQuestions))
    number = 0
    while number < 4 or number > len(quizQuestions):
       number = int(input("How many questions do you want to answer? "))
    print('\nGood luck!')
    return number

def how_to_play():
    print("You will be asked a series of questions about online safety.")
    print("Select the correct answer by typing in the number when prompted.")
    print("Each correct answer will be awarded 4 points.\n")
 
def quiz(numberOfQuestions):
    #Selects a question, making sure that each question is only asked once
     questionsAsked = [] #Keeps track of which questions have already been asked
     score = 0
     questionNumber = random.randint(0, availableQuestions)
     while len(questionsAsked) < numberOfQuestions:
        while questionNumber in questionsAsked: #Checks if question has already been asked
            questionNumber = random.randint(0, availableQuestions)
        questionsAsked.append(questionNumber)
        question = quizQuestions[questionNumber]
        print("")
        print(question[0])
 
        #Displays answers in random order
        index = 1 #Adds a question number
        answersShown = [] #Keeps track of which answers have already been displayed
        answer = random.randint(1, 3)
        while len(answersShown) < 3:
            while answer in answersShown:
                  answer = random.randint(1, 3)
            aNumb = str(index) +"."
            print(aNumb, question[answer][2:])
            #Keeps track of the correct answer
            if question[answer][:1] == "C":
                correctChoice = index
                correctAnswer = question[answer][2:]
            #print(question[answer][2:])
            answersShown.append(answer)
            index += 1

        #Checks answer and updates score if appropriate
        print("")
        chosenAnswer = 0
        while chosenAnswer < 1 or chosenAnswer > 3:
            chosenAnswer = int(input("Choose 1, 2 or 3: "))
        if chosenAnswer == correctChoice:
            print("Well done. That's the correct answer.")
            score += 4
        else:
            print("Is incorrect. The correct answer is:", correctAnswer)
 
     return score

def result(name, score, number):
    print("-" * 60)
    print("\nThat's the end of the quiz.\n")
    print(name, "you scored", score, "out of a possible", number * pointsPerQuestion, "points.\n")
     
##Main program
print("\nOnline Safety quiz")
print("-" * 18)
playerName = new_player()
how_to_play()
anotherGo = "y"
while anotherGo.lower() == "y":
    number = how_many_questions()
    playerScore = quiz(number)
    result(playerName, playerScore, number)
    anotherGo = input("Another go (y/n)? ")
print("\nThanks for playing. Goodbye!")

    
    
