DEV Community

Palak Hirave
Palak Hirave

Posted on

Day 12 of 100

Today I learnt about namespaces and global vs local variables. Now I had already stumbled upon this consept during some casual reading so I had a basic idea of what their usecases were. For today's project, I built a Guess the Name game. It's a text based version that allows the user to guess any number from 1 to a 100. If they choose the easy mode they get 10 chances but if they choose the hard mode they get only 5. Unlike yesterday's challenge today's one was fairly easy and I managed to wrap it up in around 30 mins. For this one I didn't write a plan or algorim as I had played this game many times and knew it's simple logistics.

Here's the program:

import random
import art

random_number = random.randint(1, 101)

print(art.logo)
print("Welcome to the Number Guessing Game")

print("I am thinking of a number between 1 and 100")
level = str(input("Do you want the easy or hard mode? Type 'easy' or 'hard': "))
level = level.lower()

def compare():
    if random_number == guess:
        print("You guessed the number!")
        return 0
    elif random_number > guess:
        print("Too low!")
    elif random_number < guess:
        print("Too high!")
    else:
        print("Please type a valid input")

if level == "easy":
    chance = 10
else:
    chance = 5

while chance > 0:
    guess = int(input("Guess a number between 1 and 100: "))
    chance -= 1

    if compare() == 0:
        chance = 0
        print("Good job!")
        break

    print(f"You have {chance} chances left")

    if chance == 0:
        print(f"You lost, the number was {random_number}. Refresh the page to try again.")

Enter fullscreen mode Exit fullscreen mode

The imported art file was just another file containing an ASCII Art. I had done it though this cool website I found that coverts text into ASCII with a large selection of fonts and styles.

Top comments (0)