How do you change a variable in a function to then be used in the main code?
<p>I am quite new to Python and I am having some trouble figuring out the following:</p>

<pre><code>import random
import sys

print("Welcome to this Maths quiz.")
playerName = str(input("Please enter your name: "))
playerAge = int(input("Please enter your age: "))
if playerAge &lt; 11:
    print("This quiz is not for your age.")
    sys.exit(0)
else :
    print("Great! Let's begin.\n")

quizQuestions = ["9(3+8)", "7+9*8", "(9+13)(9-5)", "50*25%", "104-4+5*20"]
quizAnswers = ["99", "79", "88", "12.5", "0"]
quizSync = list(zip(quizQuestions, quizAnswers))
random.shuffle(quizSync)
quizQuestions, quizAnswers = zip( * quizSync)
questionNumber = 1
quizScore = 0

def displayQuestion(quizQuestions, quizAnswers, questionNumber, quizScore):
    print("Question " + str(questionNumber) + ": " + quizQuestions[questionNumber - 1] + "\n")
    questionAnswer = str(input())
    if questionAnswer == quizAnswers[questionNumber - 1]:
        print("\nCorrect!\n")
        quizScore += 1
    else :
        print("\nIncorrect! The answer is: " + quizAnswers[questionNumber - 1] + "\n")

while questionNumber &lt; 6:
    displayQuestion(quizQuestions, quizAnswers, questionNumber, quizScore)
    questionNumber += 1

print("You have a total score of: "+str(quizScore))
</code></pre>

<p>I would like the variable "quizScore" in the function "displayQuestion" to increase by one if the player gets a question right. However, after the quiz is finished, the print function at the end always prints the score is 0 even if the player gets questions right.</p>

<p>Although this won't be the shortest answer, which is to use another global variable. It instead will show you how to avoid using global variables (<a href="http://c2.com/cgi/wiki?GlobalVariablesConsideredHarmful" rel="nofollow">which are considered harmful</a>) by using <a href="https://en.wikipedia.org/wiki/Object-oriented_programming" rel="nofollow">Object Oriented Programming (OOP)</a>. To accomplish this, most of the code in your question can be encapsulated into a single class named <code>MathQuiz</code> below.</p>

<p>Besides getting rid of almost all global variables, it also provides a usable template for you to create any number of independent math quizzes.</p>

<pre><code>import random
import sys

class MathQuiz:
    def __init__(self, questions, answers):
        quizSync = list(zip(questions, answers))
        random.shuffle(quizSync)
        self.quizQuestions, self.quizAnswers = zip(*quizSync)
        self.quizScore = 0

        print("Welcome to this Maths quiz.")
        self.playerName = str(input("Please enter your name: "))
        self.playerAge = int(input("Please enter your age: "))
        if self.playerAge &gt; 10:
            print("Great! Let's begin.\n")
        else :
            print("This quiz is not for your age.")
            sys.exit(0)

    def run(self):
        for questionNumber in range(len(self.quizQuestions)):
            self._displayQuestion(questionNumber)

        print("You have a total score of: " + str(self.quizScore))

    def _displayQuestion(self, questionNumber):
        print("Question " + str(questionNumber) + ": "
              + self.quizQuestions[questionNumber-1]
              + "\n")
        questionAnswer = str(input())
        if questionAnswer == self.quizAnswers[questionNumber-1]:
            print("\nCorrect!\n")
            self.quizScore += 1
        else :
            print("\nIncorrect! The answer is: "
                  + self.quizAnswers[questionNumber-1]
                  + "\n")

quiz = MathQuiz(["9(3+8)", "7+9*8", "(9+13)(9-5)", "50*25%", "104-4+5*20"],
                ["99", "79", "88", "12.5", "0"])
quiz.run()
</code></pre>

python function variables