Learning Python through Console Based Applications (For Beginners)

Hi Peeps

So glad you could join me.

I’ve started a new tutorial series on learning Python through building console based applications.

Below is the starter code for the application that we’ll be working with for the duration of this series. This is a series for beginners, by beginners. As we go along and acquire new skills, our code will most definitely change.

In the meantime, feel free to copy the code below into your own application. We’ll be discussing the code in more detail in tutorials to come.

As always, if you have any comments or suggestions, feel free to reach out in the comment section below.

Until next time…

Code:

## ----------------------------------------------- Setup -----------------------------------------------

## -------- Libraries / Modules --------

# Import the Operating System Library
import os

## -------- Custom Funtions --------

# Clears the console window when executed (Windows CMD)
def clear():
    os.system('cls')

# The Main Menu of this Program
def mainMenu():
    clear()
    print()
    print("*************************************************************************")
    print("*\t\t\t\tMain Menu\t\t\t\t*")
    print("*************************************************************************")
    print("*\t\tPlease select an option from the Menu below\t\t*")
    print("*\t\t\t\t\t\t\t\t\t*")
    print("*  1. Working with Strings\t\t\t\t\t\t*")
    print("*  2. Working with Numbers\t\t\t\t\t\t*")
    print("*  3. Working with Lists\Arrays\t\t\t\t\t\t*")
    print("*  4. Working with Working with Dictionaries\t\t\t\t*")
    print("*  5. Quit Application\t\t\t\t\t\t\t*")
    print("*\t\t\t\t\t\t\t\t\t*")
    print("*************************************************************************")
    
    # Ask the user for his/her selection
    userInput = input("Enter selection: ")
    
    # Call the 'case' function with the user's input as argument
    case(userInput)

# This the main Menu case statement (dictionary) to switch between functions
def case(arg):
    switch = {
        '1': workingWithStrings,
        '2': workingWithNumbers,
        '3': workingWithArrays,
        '4': workingWitDictionaries,
        '5': quitApplication
    }
    
    # Get the function associated with the user's input 
    switchResult = switch.get(arg,mainMenu)
    
    # If the function is a callable function then execute as functionif callable(switchResult):
        switchResult = switchResult()
    
    # If it is not a callable function, output the selection as a string.# In this program, the following code will not execute due to the 'mainMenu' function# being passed as a second argument in the 'switch.get function'.  If the user does not make a# selection from the list, or makes an incorrect selection, the program will loop back to the Main Menu.else:
        print(switchResult)

# Go back to Main Menu or Quit the application based on user-input
def toMainMenu():
    menuInput = input("Continue to Main Menu Y/N: ")
    
    # If the user selects "y" / "Y" the program will go back to the Main Menuif menuInput.lower() == 'y':
        mainMenu()
    
    # If the user selects "n" / "N" the program will quitelif menuInput.lower () == 'n':
        os.system('cls')
        quit()
    
    # If the user makes any other selection, the user will be promted to select "y"/"n"else:
        toMainMenu()

# Selection 1. Working with Strings
def workingWithStrings():
    clear()
    print("You are working with Strings")
    print()
    toMainMenu()

# Selection 2. Working with Numbers
def workingWithNumbers():
    clear()
    print("You are working with Numbers")
    print()
    toMainMenu()

# Selection 3. Working with Arrays
def workingWithArrays():
    clear()
    print("You are working with Arrays")
    print()
    toMainMenu()

# Selection 4. Dictionaries
def workingWitDictionaries():
    clear()
    print("You are working with Dictionaires")
    print()
    toMainMenu()

# Selection 5. Quit the application
def quitApplication():
    clear()
    quit()

## ----------------------------------------------- Main Loop -----------------------------------------------

# The Main Menu laods once the application is launced
mainMenu()

Leave a Reply

Your email address will not be published. Required fields are marked *