Best Tricky Intermediate Level Python Projects (with Source Code) - Whoopee Webs



In the world where AI, ML, and Data Science are the dominant technologies these days, all the mentioned technologies are dependent on the Python programming language in some or the other way. So becoming a master in Python can open many doors in your career and land in some of the best opportunities across the planet. 
I am hoping you as a reader here is either beginning in Python or practicing to become an expert or maybe challenging your skills with even harder problems to work on with Python. No matter wherever you rate yourself in the Python skill, trying to work on Python projects would definitely uplift your skills and build up your profile to face the competitive world outside. Although, Python books and Python tutorials are pretty helpful to and provide quite detailed knowledge of the ultimate test of your learning would come from the capability that you can code and create something of your own. 
Before jumping right into the project ideas let us read how can Python projects help you as a Python developer and which platform you should consider before you start any Python projects.



 1. CALCULATOR



Building this project you would learn to design a graphical UI and make you familiar with a library like Tkinter. This library enables you to create buttons to perform different operations and display results on the screen.


SOURCE CODE

""" Calculator
----------------------------------------
"""
def addition ():
    print("Addition")
    n = float(input("Enter the number: "))
    t = 0 //Total number enter
    ans = 0
    while n != 0:
        ans = ans + n
        t+=1
        n = float(input("Enter another number (0 to calculate): "))
    return [ans,t]
def subtraction ():
    print("Subtraction");
    n = float(input("Enter the number: "))
    t = 0 //Total number enter
    sum = 0
    while n != 0:
        ans = ans - n
        t+=1
        n = float(input("Enter another number (0 to calculate): "))
    return [ans,t]
def multiplication ():
    print("Multiplication")
    n = float(input("Enter the number: "))
    t = 0 //Total number enter
    ans = 1
    while n != 0:
        ans = ans * n
        t+=1
        n = float(input("Enter another number (0 to calculate): "))
    return [ans,t]
def average():
    an = []
    an = addition()
    t = an[1]
    a = an[0]
    ans = a / t
    return [ans,t]
// main...
while True:
    list = []
    print(" My first python program!")
    print(" Simple Calculator in python by Malik Umer Farooq")
    print(" Enter 'a' for addition")
    print(" Enter 's' for substraction")
    print(" Enter 'm' for multiplication")
    print(" Enter 'v' for average")
    print(" Enter 'q' for quit")
    c = input(" ")
    if c != 'q':
        if c == 'a':
            list = addition()
            print("Ans = ", list[0], " total inputs ",list[1])
        elif c == 's':
            list = subtraction()
            print("Ans = ", list[0], " total inputs ",list[1])
        elif c == 'm':
            list = multiplication()
            print("Ans = ", list[0], " total inputs ",list[1])
        elif c == 'v':
            list = average()
            print("Ans = ", list[0], " total inputs ",list[1])
        else:
            print ("Sorry, invilid character")
    else:
        break




 2. ALARM CLOCK


This is an interesting Command Line Interface (CLI) Python application for an intermediate-level developer. People across the globe use alarm clock features in their devices but this project can be altered in a bit different manner. Some certain YouTube links can be added to a text file and the project is programmed in a way that when a user sets an alarm then the code shall pick a random link from the video and will start playing the YouTube link.


SOURCE CODE

""" Alarm Clock
----------------------------------------
"""
import datetime
import os
import time
import random
import webbrowser
// If video URL file does not exist, create one
if not os.path.isfile("youtube_alarm_videos.txt"):
    print('Creating "youtube_alarm_videos.txt"...')
    with open("youtube_alarm_videos.txt", "w") as alarm_file:
        alarm_file.write("https://www.youtube.com/watch?v=anM6uIZvx74")
def check_alarm_input(alarm_time):
    """Checks to see if the user has entered in a valid alarm time"""
    if len(alarm_time) == 1: // [Hour] Format
        if alarm_time[0] < 24 and alarm_time[0] >= 0:
            return True
    if len(alarm_time) == 2: // [Hour:Minute] Format
        if alarm_time[0] < 24 and alarm_time[0] >= 0 and \
           alarm_time[1] < 60 and alarm_time[1] >= 0:
            return True
    elif len(alarm_time) == 3: // [Hour:Minute:Second] Format
        if alarm_time[0] < 24 and alarm_time[0] >= 0 and \
           alarm_time[1] < 60 and alarm_time[1] >= 0 and \
           alarm_time[2] < 60 and alarm_time[2] >= 0:
            return True
    return False
// Get user input for the alarm time
print("Set a time for the alarm (Ex. 06:30 or 18:30:00)")
while True:
    alarm_input = input(">> ")
    try:
        alarm_time = [int(n) for n in alarm_input.split(":")]
        if check_alarm_input(alarm_time):
            break
        else:
            raise ValueError
    except ValueError:
        print("ERROR: Enter time in HH:MM or HH:MM:SS format")
// Convert the alarm time from [H:M] or [H:M:S] to seconds
seconds_hms = [3600, 60, 1] // Number of seconds in an Hour, Minute, and Second
alarm_seconds = sum([a*b for a,b in zip(seconds_hms[:len(alarm_time)], alarm_time)])
// Get the current time of day in seconds
now = datetime.datetime.now()
current_time_seconds = sum([a*b for a,b in zip(seconds_hms, [now.hour, now.minute, now.second])])
// Calculate the number of seconds until alarm goes off
time_diff_seconds = alarm_seconds - current_time_seconds
// If time difference is negative, set alarm for next day
if time_diff_seconds < 0:
    time_diff_seconds += 86400 // number of seconds in a day
// Display the amount of time until the alarm goes off
print("Alarm set to go off in %s" % datetime.timedelta(seconds=time_diff_seconds))
// Sleep until the alarm goes off
time.sleep(time_diff_seconds)
// Time for the alarm to go off
print("Wake Up!")
// Load list of possible video URLs
with open("youtube_alarm_videos.txt", "r") as alarm_file:
    videos = alarm_file.readlines()
// Open a random video from the list
webbrowser.open(random.choice(videos))




 3. TIC-TAC-TOE


This game is very popular amongst all of us and even fun to build as a Python project. I am pretty sure most of us know how to play it but let me give a quick brush up. 
It is a two-player game and consists of a nine-square grid. Each player chooses their move and with O or X and marks their square one at each chance. The player who succeeds in making their marks all in one line whether diagonally, horizontally, or vertically wins. The challenge for the other player is to block the game for their opponent and also to make their chain. 
For building this project in Python who can use the Pygame Python library that is loaded with all computer graphics and sounds.


SOURCE CODE

""" Tic Tac Toe
----------------------------------------
"""
import random
import sys
board=[i for i in range(0,9)]
player, computer = '',''
// Corners, Center and Others, respectively
moves=((1,7,3,9),(5,),(2,4,6,8))
// Winner combinations
winners=((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
// Table
tab=range(1,10)
def print_board():
    x=1
    for i in board:
        end = ' | '
        if x%3 == 0:
            end = ' \n'
            if i != 1: end+='---------\n';
        char=' '
        if i in ('X','O'): char=i;
        x+=1
        print(char,end=end)
def select_char():
    chars=('X','O')
    if random.randint(0,1) == 0:
        return chars[::-1]
    return chars
def can_move(brd, player, move):
    if move in tab and brd[move-1] == move-1:
        return True
    return False
def can_win(brd, player, move):
    places=[]
    x=0
    for i in brd:
        if i == player: places.append(x);
        x+=1
    win=True
    for tup in winners:
        win=True
        for ix in tup:
            if brd[ix] != player:
                win=False
                break
        if win == True:
            break
    return win
def make_move(brd, player, move, undo=False):
    if can_move(brd, player, move):
        brd[move-1] = player
        win=can_win(brd, player, move)
        if undo:
            brd[move-1] = move-1
        return (True, win)
    return (False, False)
// AI goes here
def computer_move():
    move=-1
    // If I can win, others do not matter.
    for i in range(1,10):
        if make_move(board, computer, i, True)[1]:
            move=i
            break
    if move == -1:
       // If player can win, block him.
        for i in range(1,10):
            if make_move(board, player, i, True)[1]:
                move=i
                break
    if move == -1:
        // Otherwise, try to take one of desired places.
        for tup in moves:
            for mv in tup:
                if move == -1 and can_move(board, computer, mv):
                    move=mv
                    break
    return make_move(board, computer, move)
def space_exist():
    return board.count('X') + board.count('O') != 9
player, computer = select_char()
print('Player is [%s] and computer is [%s]' % (player, computer))
result='%%% Deuce ! %%%'
while space_exist():
    print_board()
    print('#Make your move ! [1-9] : ', end='')
    move = int(input())
    moved, won = make_move(board, player, move)
    if not moved:
        print(' >> Invalid number ! Try again !')
        continue
    //
    if won:
        result='*** Congratulations ! You won ! ***'
        break
    elif computer_move()[1]:
        result='=== You lose ! =='
        break;
 print_board()
 print(result)




 4. DIRECTORY TREE GENERATOR



This project is useful for visualizing the relationship between files and directories and making their positioning easy to comprehend. Python OS library can be used to list the files and directories within a specific directory. The excellent frameworks of this project are Docopt and Argparse.


SOURCE CODE

""" Directory Tree Generator
----------------------------------------
"""
import argparse
import os
from walkdir import filtered_walk
parser = argparse.ArgumentParser(description='Print the directory-tree code for the LaTeX dirtree package.')
parser.add_argument(dest='path', type=str, help="Root directory of the tree")
parser.add_argument('-d', '--maxDepth', dest='maxDepth', type=int, help="Max depth for tree expansion")
parser.add_argument('-H', '--includeHidden', dest='includeHidden', action='store_true', help='Include hidden files')
parser.add_argument('-S', '--includeSystem', dest='includeSystem', action='store_true', help='Include system files')
system_file_names = [".DS_Store"]
// Delete trailing / in rootDir which can lead to errors
def delete_trailing_slash(path_name):
    while path_name.endswith('/'):
        path_name = path_name[:-1]
    return path_name
// Count how many levels deep is the directory with respect to dirRoot
def get_relative_depth(dir_path, level_offset):
    return dir_path.count(os.path.sep) - level_offset
// Escape illegal symbols for LaTeX
def escape_illegal(name):
    illegal_char_array = ['\\', '&', '%', '$', '#', '_', '{', '}', '~', '^']
    for char in illegal_char_array:
        name = name.replace(char, "\\" + char)
    return name
rootDir = delete_trailing_slash(parser.parse_args().path)
includeHidden = parser.parse_args().includeHidden
includeSystem = parser.parse_args().includeSystem
maxDepth = parser.parse_args().maxDepth
// if the directory exists
if os.path.isdir(rootDir) and os.path.exists(rootDir):
    indentChar = " "
    // Depth of the root (i.e. number of "/")
    levelOffset = rootDir.count(os.path.sep) - 1
    // Create filter
    excluded_filter = []
    if not includeHidden:
        excluded_filter.append(".*")
    if not includeSystem:
        excluded_filter += system_file_names
    print ("\dirtree{%")
    for dirName, subdirList, fileList in sorted(filtered_walk(rootDir, depth=maxDepth, excluded_dirs=excluded_filter,
                                                       excluded_files=excluded_filter)):
        level = get_relative_depth(dirName, levelOffset)
        baseName = os.path.basename(dirName)
        if level == 1:  // for the first level only print the whole path
            print(indentChar + "." + str(level) + " {" + escape_illegal(dirName) + "} .")
        else:
            print(indentChar * level + "." + str(level) + " {" + escape_illegal((os.path.basename(dirName))) + "} .")
        level += 1
        for fileName in sorted(fileList):
            print(indentChar * level + "." + str(level) + " {" + escape_illegal(fileName) + "} .")
    print ("}")
else:
    print ("Error: root directory not found")




 5. CURRENCY CONVERTER



This is a straightforward project with a simple GUI. The name quite evidently describes the role of the project is to convert currencies from one unit into another. For example, converting Indian rupee to USD or euro. Tkinter, the standard Python interface can be used to design and develop this application. 


SOURCE CODE

""" Currency Converter
----------------------------------------
"""
import urllib.request
import json
def currency_converter(currency_from, currency_to, currency_input):
    yql_base_url = "https://query.yahooapis.com/v1/public/yql"
    yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair' \
                '%20in%20("'+currency_from+currency_to+'")'
    yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
    try:
        yql_response = urllib.request.urlopen(yql_query_url)
        try:
            json_string = str(yql_response.read())
            json_string = json_string[2:
            json_string = json_string[:-1]
            print(json_string)
            yql_json = json.loads(json_string)
            last_rate = yql_json['query']['results']['rate']['Rate']
            currency_output = currency_input * float(last_rate)
            return currency_output
        except (ValueError, KeyError, TypeError):
            print(yql_query_url)
            return "JSON format error"
    except IOError as e:
        print(str(e))
currency_input = 1
// currency codes : http://en.wikipedia.org/wiki/ISO_4217
currency_from = "USD"
currency_to = "TRY"
rate = currency_converter(currency_from, currency_to, currency_input)
print(rate)

Post a Comment

0 Comments