Making a number guessing game
- 라임 샹큼
- Mar 5
- 3 min read
Compared to other possible challenges, this project was measurably easy and I was quick to finish it.
import random
from art import logo
print(logo)
print('''Welcome to Numer Guessing Game!
I'm thimking of a number between 1 and 100''')
difficulty = input('Choose difficulty: easy or hard:').lower()
num = random.randint(1,100)
chances = 0
#setting up amount of chances depending on difficulty
if difficulty == 'easy':
chances = 10
elif difficulty == 'hard':
chances = 5
print(f'You have {chances}chances to guess the number!')
while chances > 0:
chances -= 1
guess = int(input('Make a guess at a number:'))
if guess < num:
print('Too low')
print(f'You have {chances} guesses left')
elif guess > num:
print('Too high')
print(f'You have {chances} guesses left')
elif guess == num:
print('You got it right!!')
print(f'The number was {num}!!')
chances = 0
#after running out of guesses
if num != guess:
print('You didn\' get it right...')
print(num)
The one thing I'd point out about this code is that it is not instantly understandable. This can be fixed by using functions.
from random import randint
from art import logo
EASY_LEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
# Function to check users' guess against actual answer
def check_answer(user_guess, actual_answer, turns):
"""Checks answer against guess, returns the number of turns remaining."""
if user_guess > actual_answer:
print("Too high.")
return turns - 1
elif user_guess < actual_answer:
print("Too low.")
return turns - 1
else:
print(f"You got it! The answer was {actual_answer}")
# Function to set difficulty
def set_difficulty():
level = input("Choose a difficulty. Type 'easy' or 'hard': ")
if level == "easy":
return EASY_LEVEL_TURNS
else:
return HARD_LEVEL_TURNS
def game():
print(logo)
# Choosing a random number between 1 and 100.
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
answer = randint(1, 100)
print(f"Pssst, the correct answer is {answer}")
turns = set_difficulty()
# Repeat the guessing functionality if they get it wrong.
guess = 0
while guess != answer:
print(f"You have {turns} attempts remaining to guess the number.")
# Let the user guess a number
guess = int(input("Make a guess: "))
# Track the number of turns and reduce by 1 if they get it wrong
turns = check_answer(guess, answer, turns)
if turns == 0:
print("You've run out of guesses, you lose.")
return
elif guess != answer:
print("Guess again.")
game()
This would be the ideal way of writing the code.
This code used global variables, capitalized at the top. The merits of using global variables are that you can change the argument whenever you want and it will change the whole code along with it without you having to go through the whole thing again to check for things to replace.
Things I learned
Local scope:
only can access variable inside the function you created it in.
def hi():
h_llo = 2
print(h_llo)
print(h_llo) -> error
Global scope:
can be used in and out of the function if defined at the top of all the code
in terms of local scope, is only defined as such if nested in a defining function.
if it is named in an if , it can be called outside of the if
global and local variabels are entirely different
enemy = 2
def love():
enemy = 1
#the enemy above and the enemy in the function are different variables
if you want to use a variable in the global scope in a defining function, you have to explicitly state 'global variablename'
enemy = 2
def love():
global enemy
enemy +=1
Usually don't use global in def but if global constants like pi, things that you won't change, can use global.
when using global constants, have the variable name in all uppercase
Comments