Beginner Friendly Python Projects That Are Fun!
Projects like this are not only beginner friendly, but they add a little bit of fun to your studies or career.
Andrey Metelev via Unsplash
I recently did an article on building a Python project in less than 5 minutes. So I thought I’d do another one, with a few more projects for beginners to play around with and test their skills.Â
If you don’t have fun in your career, you’ll soon enough lose your mojo and start hating it. Projects like this are not only beginner friendly, but they add a little bit of fun to your studies or career.Â
So let’s get started.
Guess the Number
In this first project, we will be generating a random number within a specific range which the user has to guess using hints.Â
The more guesses the user gets wrong, the more hints they will be given - however, it will reduce their score.Â
Code:
""" Guess The Number """ 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("The current high score is {} attempts".format(min(attempts_list))) def start_game(): random_number = int(random.randint(1, 10)) print("Hello traveler! Welcome to the game of guesses!") player_name = input("What is your name? ") wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name)) #Where the show_score function USED to be attempts = 0 show_score() while wanna_play.lower() == "yes": try: guess = input("Pick a number between 1 and 10 ") if int(guess) < 1 or int(guess) > 10: raise ValueError("Please guess a number within the given range") if int(guess) == random_number: print("Nice! You got it!") attempts += 1 attempts_list.append(attempts) print("It took you {} attempts".format(attempts)) play_again = input("Would you like to play again? (Enter Yes/No) ") attempts = 0 show_score() random_number = int(random.randint(1, 10)) if play_again.lower() == "no": print("That's cool, have a good one!") break elif int(guess) > random_number: print("It's lower") attempts += 1 elif int(guess) < random_number: print("It's higher") attempts += 1 except ValueError as err: print("Oh no!, that is not a valid value. Try again...") print("({})".format(err)) else: print("That's cool, have a good one!") if __name__ == '__main__': start_game()
Hangman
The whole point of hangman is choosing a word, so first we need to find a list of words. On StackOverflow, there is a JSON file that has over 2400 words. You can find it here: randomlist.Â
Using this JSON file, copy these words into a .py file and assign it to the variable ‘words’. Like this:
words = "aback","abaft","abandoned","abashed","aberrant","abhorrent"...
Code:
Create a second .py file and call it hangman.py - it will contain this:
""" Hangman """ #Imports ​​import random from words import words from hangman_visual import lives_visual_dict import string def get_valid_word(words): word = random.choice(words) # randomly chooses something from the list while '-' in word or ' ' in word: word = random.choice(words) return word.upper() def hangman(): word = get_valid_word(words) word_letters = set(word) # letters in the word alphabet = set(string.ascii_uppercase) used_letters = set() # what the user has guessed lives = 7 # getting user input while len(word_letters) > 0 and lives > 0: # letters used # ' '.join(['a', 'b', 'cd']) --> 'a b cd' print('You have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters)) # what current word is (ie W - R D) word_list = [letter if letter in used_letters else '-' for letter in word] print(lives_visual_dict[lives]) print('Current word: ', ' '.join(word_list)) user_letter = input('Guess a letter: ').upper() if user_letter in alphabet - used_letters: used_letters.add(user_letter) if user_letter in word_letters: word_letters.remove(user_letter) print('') else: lives = lives - 1 # takes away a life if wrong print('\nYour letter,', user_letter, 'is not in the word.') elif user_letter in used_letters: print('\nYou have already used that letter. Guess another letter.') else: print('\nThat is not a valid letter.') # gets here when len(word_letters) == 0 OR when lives == 0 if lives == 0: print(lives_visual_dict[lives]) print('You died, sorry. The word was', word) else: print('YAY! You guessed the word', word, '!!') if __name__ == '__main__': hangman()
Run your hangman.py file and let’s play!
Rock, Paper, Scissor
The Rock, Paper, Scissor game uses random.choice(), if statements, and getting user input working with these functions:
- Random function which is used to generate either rock, paper, or scissors.Â
- Valid function to check whether your move is valid
- Result function to check who has won that round
- Scorekeeper to keep track of the score.
Code:
# if not re.match("^[a-z]*$", input_str): import random import os import re os.system("cls" if os.name == "nt" else "clear") while 1 < 2: print("\n") print("Rock, Paper, Scissors - Shoot!") userChoice = input("Choose your weapon [R]ock, [P]aper, or [S]cissors, [E]xit: ") if userChoice == "E": break if not re.match("[SsRrPp]", userChoice): print("Please choose a letter:") print("[R]ock, [S]cissors or [P]paper.") continue # Echo the user's choice print("You chose: " + userChoice) choices = ["R", "P", "S"] opponenetChoice = random.choice(choices) print("I chose: " + opponenetChoice) if opponenetChoice == str.upper(userChoice): print("Tie!") # if opponenetChoice == str("R") and str.upper(userChoice) == "P" elif opponenetChoice == "R" and userChoice.upper() == "S": print("Scissors beats rock, I win!") continue elif opponenetChoice == "S" and userChoice.upper() == "P": print("Scissors beats paper! I win!") continue elif opponenetChoice == "P" and userChoice.upper() == "R": print("Paper beat rock, I win! ") continue else: print("You win!")