100 Days of Code: The Complete Python Pro Bootcamp for 2022 - Day 12 (Number Guessing Game)
Project 12 - The Number Guessing Game
fromrandomimportrandintfromreplitimportclearfromartimportlogo# Global scope
EASY_LEVEL_TURNS=10HARD_LEVEL_TURNS=5# Function to check users guess against answer
defcheck_answer(guess,answer,turn,):ifguess>answer:print("Too high")returnturn-1elifguess<answer:print("Too low")returnturn-1else:print(f"You got it, the answer was {answer} ")# Function to set difficulty
defset_difficulty():level=input("Choose a difficulty level. Type 'easy' or 'hard': ")iflevel=='easy':returnEASY_LEVEL_TURNSelse:returnHARD_LEVEL_TURNS# Define game function
defplay_game():print(logo)--Choosingarandomnumberbetween1and100print("Welcome to the Number Guessing Game")print("Choose a random number between 1 and 100: ")answer=randint(1,100)# Print statement used for initial testing
print(f"Psssst, the correct number is {answer}")# Repeat the guesing functionality if the answer is wrong
turns=set_difficulty()guess=0whileguess!=answer:print(f"You have {turns} attempts remaing to guess the answer")# let the player guess a number
guess=int(input("Guess again:"))# Track the number of turns and reduce by 1 if they get it wrong
turns=check_answer(guess,answer,turns,)ifturns==0:print("You've run out of turns, You Lose")returnelifguess!=answer:print(f"Guess again:")clear()play_game()
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)