๐ #100DaysOfCodeโโโDay 12: Scope & Number Guessing Game
Today I learned about scope in Python and built a fun Number Guessing Game as the project.
Wiki topics:
๐ป ยท Programming
๐ #100DaysOfCode โ Day 12: Scope & Number Guessing Game
Today I learned about scope in Python and built a fun Number Guessing Game as the project.

๐ Scope in Python
Python variables have different lifetimes depending on where they are declared.
- Local Scope โ Exists only inside functions.
def drink_potion():
potion_strength = 2
print(potion_strength) # works
drink_potion()
print(potion_strength) # error, not defined outside
- Global Scope โ Accessible everywhere.
player_health = 10
def drink_potion():
print(player_health) # works fine
- No Block Scope โ Variables in loops/ifs still exist outside.
game_level = 3
if game_level < 5:
new_enemy = "Skeleton"
print(new_enemy) # still accessible
- Modifying Global Variables โ Use
globalkeyword.
enemies = 1
def increase_enemies():
global enemies
enemies += 1
- Global Constants โ Written in ALL CAPS.
PI = 3.14159
๐ฎ Final Project: Number Guessing Game
I built a game where the computer picks a number between 1 and 100, and the player must guess it within limited attempts depending on difficulty.
import random
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
difficulty = input("Choose 'easy' or 'hard': ")
random_number = random.randint(1, 100)
def guess_check(level):
attempts = 10 if level == "easy" else 5
while attempts > 0:
print(f"You have {attempts} attempts remaining.")
guess = int(input("Make a guess: "))
if guess > random_number:
print("Too high.")
elif guess < random_number:
print("Too low.")
else:
print(f"You got it! The number was {random_number} ๐")
return
attempts -= 1
print("You've run out of guesses. You lose ๐ข")
guess_check(difficulty)
โ Concepts Learned:
- Local vs Global scope
- Constants in Python
- Modifying global variables
- Implementing game logic with loops & conditionals
๐ End of Day 12 โ Onto Day 13!
๋ฉํ๋ฐ์ดํฐ
- post_id
- a2a38b7fc2af
- slug
- 100daysofcode-day-12-scope-number-guessing-game-a2a38b7fc2af
- url
- https://medium.com/@zubairujibrinbaba/100daysofcode-day-12-scope-number-guessing-game-a2a38b7fc2af
- canonical_url
- https://medium.com/@zubairujibrinbaba/100daysofcode-day-12-scope-number-guessing-game-a2a38b7fc2af
- author_url
- https://medium.com/@zubairujibrinbaba
- status
- ok
- fetched_at
- 2026-07-18 18:00:45