Codementor Events

6 Python Projects for Beginners

Published Apr 29, 2020
6 Python Projects for Beginners

Python can be a great programming language. You can make almost anything you want.
If you are a beginner and you don't know what to do, here are some beginner projects for you to make.

1-Basic Calculator

This is by far the easiest project in the list. You ask the user to put their first number, then the operator, then their second number. Once they put all of that, the program should calculate what the user wanted.

Things you should know to make this project:

  • variables
  • float
  • basic math
  • if/else if/ else
# The user's inputs for the numbers and the operators
num1 = float(input('Enter your first number: '))
Operator = input('Enter operator: ')
num2 = float(input('Enter your second number: '))

# if Operator is (+ | - | * | /) then  print out number 1 (+ | - | * | /) number 2
if Operator == '+':
    print(num1 + num2)
elif Operator == '-':
    print(num1 - num2)
elif Operator == '/':
    print(num1 / num2)
elif Operator == '*':
    print(num1 * num2)

# if the user didn't put an operator
else:
    print('Not a valid operator')

2-Guess the Number

This game is basic. The program chooses a random number. you can adjust how high or low the numbers can be (for example: 0-50 or 1-10.) It is all up to you.

Things you should know to make this project:

  • python random module
  • while loops
  • if/else if/else
  • integers
# Python Random Module
import random

# Number of Variables
attempts = 0

# Choose a random number
number = random.randint(1, 20)
print("I am thinking of a number between 1 and 20.")

# While the player's guesses is less then 6
while attempts < 6:
    guess = input("Take a guess: ")
    guess = int(guess)

    attempts += 1

    # If the player's guess is too low
    if guess < number:
        print("Higher")

    # If the player's guess is too high
    if guess > number:
        print("Lower")
        
    # If the player won, stop the loop
    if guess == number:
        break

# If the player won
if guess == number:
    attempts = str(attempts)
    print(f"Good job! You guessed my number in {attempts} guesses!")

# If the player lost
if guess != number:
    number = str(number)
    print(f"Nope. The number I was thinking of was {number}")

3-Rock, Papers, Scissors

For me, this was the easiest game to make, The program randomly chooses rock, paper, or scissors. Then the player types his choice. Then... Well you know the rules.

Things you should know to make this project:

  • python random module
  • variables
  • if/else if/else
  • functions
  • lists
# Python Random Module
import random

# Intro
print("Rock, Paper, Scissors...")

# Function
def try_again():
  # Random Choice (Rock, Paper, or Scissors)
    R_P_S = ["Rock", "Paper", "Scissors"]
    computer = random.choice(R_P_S)

  # Player's choice
    player = input("your choice: ").lower().capitalize()

  # If the program chose rock
    if computer == "Rock":
    	# If the player chose rock
        if player == "Rock":
            print(f"I chose {computer}, you chose {player}\nit's a tie!")
        # If the player chose paper
        elif player == "Paper":
            print(f"I chose {computer}, you chose {player}\nYou win!")
        # If the player chose scissors
        elif player == "Scissors":
            print(f"I chose {computer}, you chose {player}\nI win!")

  # If the program chose paper
    elif computer == "Paper":
    	# If the player chose rock
        if player == "Rock":
            print(f"I chose {computer}, you chose {player}\nI win!")
        # If the player chose paper
        elif player == "Paper":
            print(f"I chose {computer}, you chose {player}\nIt's a tie!")
        # If the player chose scissors
        elif player == "Scissors":
            print(f"I chose {computer}, you chose {player}\nYou win!")

  # If the program chose scissors
    elif computer == "Scissors":
    	# If the player chose rock
        if player == "Rock":
            print(f"I chose {computer}, you chose {player}\nYou win!")
        # If the player chose paper
        elif player == "Paper":
            print(f"I chose {computer}, you chose {player}\nI win!")
        # If the player chose scissors
        elif player == "Scissors":
            print(f"I chose {computer}, you chose {player}\nIt's a tie") 

  # If the player wants to play again
    play_again = input("Do you want to play again? yes or no: ").lower().capitalize()
    # If the player says yes, go back to the function
    if play_again == "Yes":
        try_again()
    # If the player says no, say goodbye
    elif play_again == "No":
        print("Goodbye")

# End of function
try_again()

4-Roll The Dice

A program that acts like a virtual dice. You can make the user choose how many dices they want to roll (harder) or you can choose whatever you want and have the user to accept it (easier)

Things you chould know to make this project:

  • python random module
  • python playsound module (to make it more look like a virtual dice than some program that spits out random numbers)
  • variables
  • if/else if/else
  • while loop
  • functions
# Python Random & Playsound Modules
import random
from playsound import playsound

# Function
def again():
  # Variables
    dices = 0
    roll_again = print()
    while dices == 0:
    	# User's choice of how many dices they want to roll
        chosen_dices = int(input("How many dices do you want to roll? (from 1 to 5): "))
        # If the user choose a number between 1 and 5, break out of the loop
        if chosen_dices > 0 and chosen_dices < 6:
            break
  
    # If the user choose (any number) dice(s), generate a random number from 1 to 6 and continue until we meet the user's needs
    if chosen_dices == 1:
        playsound('Dice shake.mp3')
        playsound("Dice roll.mp3")  
        while dices < 1 :
            rolls = random.randint(1, 6)
            print(rolls)
            dices +=1

    elif chosen_dices == 2:
        playsound('Dice shake.mp3')
        playsound("Dice roll.mp3")  
        while dices < 2 :
            rolls = random.randint(1, 6)
            print(rolls)
            dices +=1

    elif chosen_dices == 3:
        playsound('Dice shake.mp3')
        playsound("Dice roll.mp3")  
        while dices < 3 :
            rolls = random.randint(1, 6)          
            print(rolls)
            dices +=1

    elif chosen_dices == 4:
        playsound('Dice shake.mp3')
        playsound("Dice roll.mp3") 
        while dices < 4 :
            rolls = random.randint(1, 6)
            print(rolls)
            dices +=1

    elif chosen_dices == 5:
        playsound('Dice shake.mp3')
        playsound("Dice roll.mp3")  
        while dices < 5 :
            rolls = random.randint(1, 6)     
            print(rolls)
            dices +=1
  
    # Asking the user if he wants to roll again
    print("Do you want to roll again?")
    while roll_again != "Yes" or roll_again != "No":
        roll_again = input("Yes | No: ").lower().capitalize()
        # If yes, then go back to the function
        if roll_again == "Yes":
            again()
            break
        # If no, then say goodbye and exit
        if roll_again == "No":
            print("Goodbye")
            break
# End of function
again()

5-Temperature Converter

A useful program to help you convert temperatures.

Things you should know to make this project:

  • python math module
  • python time module (I added it so that the user has time to read)
  • variables
  • floats
  • intermediate math
  • knowledge about temperature (you can google it)
# Python Math & Time Modules
import math
import time

# Intro
print("Welcome to the Temperature Conventer. Type C for Celsuis, F for Fahreinheit and K for Kelvin")

# Function
def again():
    try_again = print()
    # Letting the user choose the temperature and convert it to another temperature else
    User_Temperature = input("your temperature | C | F | K | ").upper()
    convert_Temperature = input("The temperature you want to convert to | C | F | K | ").upper()
  
    # If the user's intial temperature (C, F, or K) convert it to what the user wants to convert to (C, F, or K) and give him the equation
    if User_Temperature == "C":
        if convert_Temperature == "F":
            degree = float(input("enter the degree: "))
            result = (degree * 9/5) + 32
            print(f"{result}°F \nThe equation: ({degree} × 9/5) + 32 = {result}")
        elif convert_Temperature == "K":
            degree = float(input("enter the degree: "))
            result = degree + 273.15
            print(f"{result}°K \nThe equation: {degree} + 273.15 = {result}")
        elif convert_Temperature == "C":
            print("This is the same type of temperature")
            time.sleep(1)
            again()
        else:
            print("Type a temperature")
            time.sleep(1)
            again()

    elif User_Temperature == "F":
        if convert_Temperature == "C":
            degree = float(input("enter the degree: "))
            result = (degree - 32) * 5/9
            print(f"{result}°F \nThe equation: ({degree} - 32) × 5/9 = {result}")
        elif convert_Temperature == "K":
            degree = float(input("enter the degree: "))
            result = (degree - 32) * 5/9 + 273.15
            print(f"{result}°K \nThe equation: ({degree} - 32) × 5/9 + 273.15 = {result}")
        elif convert_Temperature == "F":
            print("This is the same type of temperature")
            time.sleep(1)
            again()
        else:
            print("Type a temperature")
            time.sleep(1)
            again()

    elif User_Temperature == "K":
        if convert_Temperature == "C":
            degree = float(input("enter the degree: "))
            result = degree - 273.15
            print(f"{result}°F \nThe equation: {degree} - 273.15 = {result}")
        elif convert_Temperature == "F":
            degree = float(input("enter the degree: "))
            result = (degree - 273.15) * 9/5 + 32
            print(f"{result}°K \nThe equation: ({degree} - 273.15) × 9/5 + 32 = {result}")
        elif convert_Temperature == "K":
            print("This is the same type of temperature")
            time.sleep(1)
            again()
        else:
            print("Type a temperature")
            time.sleep(1)
            again()

    else:
        print("Type a temperature")
        time.sleep(1)
        again()

  # Aking if the user wants to convert again
    while try_again != "Yes" and try_again != "No":
        print("\nDo you want to try again?")
        try_again = input("Yes | No | ").lower().capitalize()
        if try_again == "Yes":
            again()
            break
        elif try_again == "No":
            print("Goodbye")
            break

again()

6-Hangman

This by far might take the longest to make, depending on how many words you put.
The program chooses a random word from the list, then the program prints out some letters and asks the user to type the missing letters. After 6 attempts, the player loses. I'll only put 1 word so that you get the idea and the script won't be long.

Things you need to know to make this project:

  • python random module
  • functions
  • lists
  • variables
  • if/else if/else
# Python Random Module
import random

# Intro
print("Welcome to Hangman! I will choose a word and you have to guess its letters. You only have 6 attempts.")

# Function
def try_again():
  # Random chooser
    words = ["ignore"]
    word_choice = random.choice(words)
    
    # Variables
    attempts = 0
    a = False
    b = False
    c = False
    d = False
    e = False
    f = False
    g = False
    h = False
    i = False
    j = False
    k = False
    l = False
    m = False
    n = False
    o = False
    p = False
    q = False
    r = False
    s = False
    t = False
    u = False
    v = False
    w = False
    x = False
    y = False
    z = False

  # If the program chose a word, print it out with missing letters. If the user gets the letters correct, change its variable to True and print it out. Once all the letters are found, the player won
    if word_choice == "ignore":
        print("__ __ n o __ e")
        guess = input("type the missing letter: ")
        while attempts < 6:
            if guess == "i":
                i=True
                if g == True and r == True:
                    print("i g n o r e")
                    win = input(f"you won, you took {attempts} attempt(s), Do you want to play again? Yes or No: ").lower().capitalize()
                    if win == "Yes":
                        try_again()
                        break
                    elif win == "No":
                        print("Goodbye")
                        break
                elif r == True:
                    print("i __ n o r e")
                    guess = input("\ntype the missing letter: ")
                elif g == True:
                    print("i g n o __ e")
                    guess = input("\ntype the missing letter: ")
                else:
                    print("i __ n o __ e")
                    guess = input("\ntype the missing letter: ")
            elif guess == "g":
                g = True
                if i == True and r == True:
                    print("i g n o r e")
                    win = input(f"you won, you took {attempts} attempt(s), Do you want to play again? Yes or No: ").lower().capitalize()
                    if win == "Yes":
                        try_again()
                        break
                    elif win == "No":
                        print("Goodbye")
                        break
                elif r == True:
                    print("__ g n o r e")
                    guess = input("\ntype the missing letter: ")
                elif i == True:
                    print("i g n o __ e")
                    guess = input("\ntype the missing letter: ")
                else:
                    print("__ g n o __ e")
                    guess = input("\ntype the missing letter: ")
            elif guess == "r":
                r = True
                if i == True and g == True:
                    print("i g n o r e")
                    win = input(f"you won, you took {attempts} attempt(s), Do you want to play again? Yes or No: ").lower().capitalize()
                    if win == "Yes":
                        try_again()
                        break
                    elif win == "No":
                        print("Goodbye")
                        break
                elif g == True:
                    print("__ g n o r e")
                    guess = input("\ntype the missing letter: ")
                elif i == True:
                    print("i __ n o r e")
                else:
                    print("__ __ n o r e")
                    guess = input("\ntype the missing letter: ")
            else:
                print("Try Again")
                attempts += 1
                guess = input("\ntype the missing letter: ")
    		
            # If all of the player's attempts lost, game over
            if not attempts < 6:
            	game_over = input("Game Over. Do you want to play again? Yes or No: ").lower().capitalize()
                if game_over == "Yes":
                	try_again()
                elif game_over == "No":
                    print("Goodbye")

# End of function
try_again()

I hope this helps you, please comment out your programs to let everyone see it.

Discover and read more posts from Zerq'sProgramz
get started
post comments1Reply
monaxle
5 years ago

Thanks for this. You inspired me to have a go at hangman.

‘’'def hangman():
# to play the hangman word game
import webbrowser # will be used later to look up definition of word
from random_word import
RandomWords # to generate the word to be guessed. from https://pypi.org/project/Random-Word/
r = RandomWords()
word = r.get_random_word(hasDictionaryDef=“true”, minLength=5, maxLength=7, minDictionaryCount=1)
word = list(word) # cast the str to a list in order to index pop and insert later
result = word.copy() # to compare answer with as the word list above will be changing.

print("\nLet's play Hangman!")
print('-' * 19, '\n')

answer = '-' * len(word)
answer = list(
    answer)  # this an the line above provides the blank asnwer template. Made to a list for index pop and insert.
noose = 0  # the incrementor for wrong guesses

def gallows():
    # the function builds the gallows incrementally for each wrong answer
    if noose == 1:
        print('______')
    elif noose == 2:
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 3:
        print('    ____')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 4:
        print('    ____')
        print('   |    |')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 5:
        print('    ____')
        print('   |    |')
        print('   |    O')
        print('   |')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 6:
        print('    ____')
        print('   |    |')
        print('   |    O')
        print('   |    |')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 7:
        print('    ____')
        print('   |    |')
        print('   |    O')
        print('   |   \|')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 8:
        print('    ____')
        print('   |    |')
        print('   |    O')
        print('   |   \|/')
        print('   |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 9:
        print('    ____')
        print('   |    |')
        print('   |    O')
        print('   |   \|/')
        print('   |    |')
        print('   |')
        print('   |')
        print('___|___')
    elif noose == 10:
        print('    ____')
        print('   |    |')
        print('   |    O')
        print('   |   \|/')
        print('   |    |')
        print('   |   / ')
        print('   |')
        print('___|___')
    elif noose == 11:
        print('   ____')
        print('   |    |')
        print('   |    O')
        print('   |   \|/')
        print('   |    |')
        print('   |   / \ ')
        print('   |')
        print('___|___')
        x = ''.join(result)
        print('\nYOU\'RE DEAD!!! The word was', x.upper() + '.')
        wp = 'https://www.onelook.com/?w=' + x
        webbrowser.open(wp)
        hangman()

gl = []  # creates a list to populate with guessed letters
while answer != result:  # result is a copy of the original word (to be guessed) list
    guess = input('\nGuess a letter: ')
    gl.extend(guess)  # adds the guessed letter to the guessed letter list
    glj = ', '.join(gl)  # casts the list to s string as it can look nicer printed this way

    if guess not in word:
        print(
            f'\n"{guess.upper()}" is not in the word. Letters guessed so far are: {glj.upper()}. Have another go.')
        ad = ''.join(answer)  # ad is short for answerdisplay. Looks better formated as a string
        print('\n', ad.upper())
        noose += 1  # trigger for advancing build of the gallows
        gallows()  # initiates gallows function

    else:
        # while loop to check if guessed letter appears in word as index method will only find first one. pop removes
        # the first instance so if there any more of them they can be found. This means if a guessed letter
        # appears more than once in the word it will show up as many times as that in the answer meaning you don't
        # have to guess the same letter more than once! Thank you Otis for the feedback!
        while guess in word:
            guessindex = word.index(guess)  # finds index of letter guessed in word
            answer.pop(guessindex)  # removes the element at the index
            answer.insert(guessindex, guess)  # adds the correct guessed letter to the correct index
            word.pop(guessindex)  # repeat of the above
            word.insert(guessindex, '-')  # as above

        display = ''.join(answer)  # looks better formatted as a string
        print(display.upper())

print('\nWell done! You got it!', display.upper(), 'was the word.')
wp = 'https://www.onelook.com/?w=' + display  # opens up a web page with a definition of the word.
webbrowser.open(wp)
hangman()

hangman()’’’