Why scope matters
๐ Scenario: Imagine you are organizing a small party. You have a list of guests and a separate list of snacks. You want to count how many guests and snacks you have, but you need to keep these counts separate so they don't get mixed up.
๐ฏ Goal: You will create two separate counts for guests and snacks using variables inside and outside a function to see how scope affects their values.
๐ What You'll Learn
Create a list called
guests with exactly these names: 'Alice', 'Bob', 'Charlie'Create a list called
snacks with exactly these items: 'Chips', 'Cookies'Create a variable called
guest_count and set it to 0Create a function called
count_snacks that creates a variable snack_count and sets it to the length of snacksInside the function
count_snacks, print the value of snack_countOutside the function, print the value of
guest_count๐ก Why This Matters
๐ Real World
Understanding scope helps you write programs where different parts do not interfere with each other, like keeping guest and snack counts separate at a party.
๐ผ Career
Scope is a basic concept in programming that helps prevent bugs and makes code easier to read and maintain, important for all software development jobs.
Progress0 / 4 steps