0% found this document useful (0 votes)
10 views

Python Project (Number Game)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Project (Number Game)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Number guessing game

This beginner Python project is a fun game that generates a


random number (in a certain range) that the user must guess
after receiving hints. For each wrong guess the user makes,
they receive extra hints but at the cost of reducing their final
score . This program is a great way to experiment with the
Python standard library, as it uses the Python random
module to generate random numbers. You can also get some
hands-on practice with conditional statements, print
formatting, user-defined functions, and various Python
operators.

import random

attempts_list = []

def show_score():
if not attempts_list:
print('There is currently no high score,'
' it\'s yours for the taking!')
Python project Page 1
Number guessing game

else:
print(f'The current high score is'
f' {min(attempts_list)} attempts\U0001F600')

def start_game():
attempts = 0
rand_num = random.randint(1, 10)
print('\U0001F44B Hello traveler! Welcome to the game
of guesses!')
print("\r")
player_name = input('What is your name? ')
wanna_play = input(
f'\U0001F44B Hi, {player_name}, would you like to play
the guessing game? \n(Enter Yes/No): ')

if wanna_play.lower() != 'yes':
print("Ok , thank you° \U0001F642 ")
exit()
else:
show_score()

while wanna_play.lower() == 'yes':


try:
print("-----------------------------------------------")
guess = int(input('Pick a number between 1 to 10: '))
if guess < 1 or guess > 10:
raise ValueError(
'Please guess a number within the given range')

Python project Page 2


Number guessing game

attempts += 1
attempts_list.append(attempts)

if guess == rand_num:
print("--------------------------------------------------")
print('\U0001F44F Nice! You got it!')
print(f'It took you {attempts} attempts')
print("------------------------------------------------")
wanna_play = input(
'Would you like to play again? (Enter Yes/No): ')
if wanna_play.lower() != 'yes':
print('\U0001F642 That\'s cool, have a good
one!')
break
else:
attempts = 0
rand_num = random.randint(1, 10)
show_score()
continue
else:
if guess > rand_num:
print('It\'s lower')
elif guess < rand_num:
print('It\'s higher')

except ValueError as err:


print('\U0001F62E Oh no!, that is not a valid value.
Try again...')
print(err)

Python project Page 3


Number guessing game

if _name_ == '_main_':
start_game()

Python project Page 4


Number guessing game

Python project Page 5

You might also like