Python 8.4: Managing a score
We continue the quiz project by adding a score, and we take the opportunity to talk about global variables in Python. The first step is to declare a score = 0 variable above the function calls and to print it at the end of the program. The harder question is: how does the ask_question function update this score?
From returning a boolean to using global
A natural idea is to make the function return True when the answer is correct and return False otherwise. To keep both the user feedback (print "good"/"wrong") and the result, we introduce a local boolean correct_answer = False at the top of the function, switch it to True in the matching branch, print accordingly and return its value. The main program then does if ask_question(...): score += 1.
The video also shows the alternative based on the global keyword. If we want to modify score directly from inside the function, Python forces us to declare global score at the top of the function body. Without it we get an error when trying to reassign the variable. With it, the function can read and write the same score that lives in the main program.
score = 0
def ask_question(question, r1, r2, r3, r4, choice_answer):
global score
# ... print question and answers ...
answer = input("Your answer: ")
if answer == choice_answer:
print("Good answer")
score += 1
else:
print("Wrong answer")
ask_question(...)
print("This is the score:", score)
An important nuance shown at the end of the video: a function can already read a global variable without the global keyword. The keyword is only required when we want to rewrite or reassign it. With this, the quiz now displays the final score after the last question, and the project is ready to be improved further in the next videos.
Summary
This lesson demonstrates how to implement score management in a Python quiz application by initializing a score variable, implementing proper conditional logic to track correct answers, and understanding Python's `global` keyword. The lesson emphasizes that `global` is required only when reassigning values to variables from the main program scope within a function, whereas reading a global variable works without declaration.
Key points
- Initialize a score variable at the program level (outside functions) set to 0, and increment it when answers are correct.
- Python's `global` keyword is required only when modifying (reassigning) a variable from the main scope inside a function.
- Reading a global variable inside a function works without the `global` keyword; declaration is needed only for reassignment.
- Declare `global variable_name` at the beginning of a function to signal that you intend to modify an outer scope variable.
- Proper score management avoids redundant variable declarations and centralizes score logic in one location.
- Python enforces strict scope rules; attempting to reassign a variable without `global` raises a scope error.
FAQ
Do I need the `global` keyword to read a variable from the main scope inside a function?
No. You only need `global` when you want to modify or reassign a variable from the main scope. Reading it works without the keyword.
Why use the `global` keyword instead of creating local variables within each function?
Using `global` prevents redundancy and avoids declaring the same score variable multiple times. This keeps score management centralized, making the code more maintainable and avoiding duplication.
What happens if you try to reassign a variable without declaring it `global` first?
Python raises a scope error because the function cannot find the variable in the local scope. You must declare `global variable_name` before reassigning any outer scope variable within a function.