โ† Back to list

๐Ÿ #100DaysOfCodeโ€Šโ€”โ€ŠDay 12: Scope & Number Guessing Game

Today I learned about scope in Python and built a fun Number Guessing Game as the project.

Zubairu Jibrin Baba ยท 2025-09-23 23:18 ยท 0 claps ยท 1.2 min read
#programming #python #coding #100daysofcode #guessing-games
Open on Medium โ†—
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 global keyword.
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