0
0
Pythonprogramming~15 mins

Global keyword in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Global Keyword in Python
๐Ÿ“– Scenario: Imagine you are creating a simple program to count how many times a button is clicked. The count needs to be updated inside a function but stored outside so it can be used anywhere in the program.
๐ŸŽฏ Goal: You will build a program that uses a global variable to keep track of the count and updates it inside a function using the global keyword.
๐Ÿ“‹ What You'll Learn
Create a global variable called click_count with initial value 0
Create a function called increment_click that increases click_count by 1
Use the global keyword inside the function to modify the global variable
Print the value of click_count after calling the function twice
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Global variables are useful when you want to keep track of information that many parts of your program need to access and update, like scores in a game or settings in an app.
๐Ÿ’ผ Career
Understanding how to use global variables and the <code>global</code> keyword helps you manage data across different parts of your code, a skill important in many programming jobs.
Progress0 / 4 steps
1
Create the global variable
Create a global variable called click_count and set it to 0.
Python
Need a hint?

Think of click_count as a box outside any function that holds the number 0.

2
Define the function to update the count
Define a function called increment_click that uses the global keyword to access click_count and adds 1 to it.
Python
Need a hint?

Inside the function, tell Python you want to use the global click_count by writing global click_count.

3
Call the function twice
Call the function increment_click two times to increase the count.
Python
Need a hint?

Just write increment_click() two times, one after the other.

4
Print the final count
Print the value of click_count to show how many times the function was called.
Python
Need a hint?

Use print(click_count) to show the number 2 on the screen.