Creating a number guessing game
import random
attempts_list = []
def show_score():
if len(attempts_list) <= 0:
print("There is currently no high score, it's yours for the taking!")
else:
print(f"The current high score is {min(attempts_list)} attempts")
def start_game(x):
random_number = int(random.randint(1, x))
print("Hello there! Welcome to the guessing game!")
player_name = input("What is your name? ")
wanna_play = input(f"Hi, {player_name}, would you like to play the guessing game? (Enter Yes/No) ")
# Show score at start of the game
attempts = 0
show_score()
while wanna_play.lower() == "yes":
try:
guess = input(f"Pick a number between 1 and {x}: ")
if int(guess) < 1 or int(guess) > x:
raise ValueError("Please guess a number within the given range")
if int(guess) == random_number:
print(f"Nice! You got it {player_name}!")
attempts += 1
attempts_list.append(attempts)
print(f"It took you {attempts} attempts")
play_again = input(f"{player_name} would you like to play again? (Enter Yes/No) ")
attempts = 0
show_score()
random_number = int(random.randint(1, x))
if play_again.lower() == "no":
print(f"That's cool {player_name}, have a good one!")
break
elif int(guess) > random_number:
print(f"Too high {player_name} come down!")
attempts += 1
elif int(guess) < random_number:
print(f"Too low {player_name} come up!")
attempts += 1
except ValueError as err:
print(f"Oh no!, that is not a valid value {player_name}. Try again...")
print(f"{err}")
else:
print(f"That's cool, have a good one {player_name}!")
if __name__ == '__main__':
start_game(100)
If you need assistance with your projects feel free to email me at info@airgad.com or whatsapp Jesse stay safe!