Python

Python Basics Project Tutorial

Project of Python Basics Tutorials

This is a final project of the Python basics tutorial section. And as such, it covers only aspects of what has been taught in the basics section of the Python coding tutorials.

The project offers practical experience of creating a simple project of the programming language.

This means that the project does not intend to offer the most efficient and effective way of developing such application. But instead, provide a sense of coding and following logical flow of thought.

In real scenarios, a developer must first understand the problem, whether it is a potential improvement or risk mitigation of the business. And as such, a variety of information needs to be comprehended beforehand. For instance, what does the company need, what would work and what not, where to find the right data, what are the best tools, and so on and so forth.

Project Virtual Assistant

The project intends to develop a simple virtual assistant. Of course, the application is not an actual virtual assistant. It is instead a logical set of conditions and statements, representing an appearance of a virtual assistant.

In real-world projects, there are plans of how to do the project and what to use. Here, the project just unfolds by following a simple question of, “what do I know to improve it.”

The project will get more and more complicated with the upcoming intermediate and advanced tutorials.

In this scenario, there is a welcome message and 4 options (actions). The operations behind the virtual assistant options are loops and conditions.

The 4 actions are:

  • Print a poem
  • Print the lucky numbers
  • Create a file
  • Exit program

The full code is at the bottom of the page.

Welcome message

The first part is very straightforward. A welcome message can simply be string text through the use of “print()” statements.

With this, the 4 options are also set.

The “print(“”)” adds empty rows in order to improve readability.

# Welcome message 

print("Hello! I am your virtual assistant, and I am here to help you.")

print("")
print("There are 4 options to choose from. Please choose the relevant option you would like help with.")
print("")
print("1. Print a poem.")
print("2. Print the lucky numbers.")
print("3. Create a file.")
print("4. Exit program.")
print("")

Question options

The next stage sets the question options. It creates the main question regarding all options of the VA, which operates as a main menu.

The four options can be conditional statements, alongside input functions, as the options require the user to choose.

# First question of all 4 options
questions = input("Choose a number option: ")
print("")

# Condition to choose option 1
if questions == "1":

# Condition to choose option 2    
elif questions == "2":

# Condition to choose option 3    
elif questions == "3":

# Condition to choose option 4
elif questions == "4":

# Condition if the user inputs a different number or character  
else:
    print("Please only choose a number between 1 and 3")

Main program switch

The program runs on a while loop, and as such, it needs to meet a condition in order to stop executing.

To run the program constantly as well as stop until needed, some sort of a on/off switch can be implemented. A room’s lights switch can represent the same idea as the while loop. When the switch is on, the lights are on. Similarly, when the switch is off, the lights are off.

The while loop operates the same way, as long as a condition is true, the loop will operate. And as soon as the condition becomes false, the while loop stops.

The condition can be anything, such as:

switch = True
switch = False

switch = "on"
switch = "off"

switch = 1
switch = 0

In this scenario, the switch’s name is “running” and True and False represent on and off, respectively.

The following implements the while loop and the switch. Conditional statements go inside the loop.

# Setting an on/off switch of the program 
running = True
# While loop to keep going until the condition of running is False
while running == True:
    
    # First question of all 4 options
    questions = input("Choose a number option: ")
    print("")
    
    # Condition to choose option 1
    if questions == "1":

    # Condition to choose option 2    
    elif questions == "2":

    # Condition to choose option 3    
    elif questions == "3":

    # Condition to choose option 4
    elif questions == "4":

    # Condition if the user inputs a different number or character  
    else:
        print("Please only choose a number between 1 and 4")

Each option

The main structure of the program is ready.

The above welcome message, while loop, switch, and conditional statements operate the whole application.

Now it’s time to develop each option (action) separately.

Print a poem (option 1)

The first action is very easy, as it simply prints a string. The poem is stored in a variable outside the while loop.

The triple quotes help the program write longer text and take into consideration new lines.

# Stored poem (option 1)
poem = """Some say the world will end in fire,
some say in ice.
From what I've tasted of desire,
I hold with those who favour fire."""
# Condition to choose option 1
if questions == "1":
    print("Great! Let's print a poem.")
    print("")
    print(poem)

Print the lucky numbers (option 2)

To make the program more interesting, instead of printing a list of numbers, the second option will call a function (together with a “for” loop) with the numbers.

The function is outside the while loop. The code of the function does not output anything until called.

# Function (option 2)
def numbers():
    numbers = [6, 1, 17, 14, 5]
    for num in numbers:
        print(num)
# Condition to choose option 2    
elif questions == "2":
    print("Great! Let's do some numbers.")
    numbers()

Create a file (option 3)

The third action is very simple as well.

It creates a file, using the mode create (“x”).

    # Condition to choose option 3    
    elif questions == "3":
        print("Great! Let's create a file.")
        
        x = open("file.txt", "x")
        print("A file has been created.")
        x.close()

Exit program (option 4)

The final fourth action allows the user to exit the application.

This is where the switch comes to play its part.

After choosing the 4th option, the variable “running” equals to False, and hence turns off the condition for the while loop. This in turn, stops the while loop executing, and hence the program.

# Condition to choose option 4
elif questions == "4":
    print("")
    print("Goodbye!")
    running = False

Project program

The following is the full code, with all pieces together.

Note that the program does not work perfectly, as it intends to improve with the further tutorials.

For instance, when the application runs successfully the first time with option 3, it creates a file. But when it runs again, after the file exists, option 3 outputs an error. The reason behind this is that the create mode “x” always produces an error in the case of an already existing file.

In the next part of the project, such issues are tackled.

Full code

# Welcome message 

print("Hello! I am your virtual assistant, and I am here to help you.")

print("")
print("There are 4 options to choose from. Please choose the relevant option you would like help with.")
print("")
print("1. Print a poem.")
print("2. Print the lucky numbers.")
print("3. Create a file.")
print("4. Exit program.")
print("")


# Stored poem (option 1)
poem = """Some say the world will end in fire,
some say in ice.
From what I've tasted of desire,
I hold with those who favour fire."""

# Function (option 2)
def numbers():
    numbers = [6, 1, 17, 14, 5]
    for num in numbers:
        print(num)


# Setting an on/off switch of the program 
running = True
# While loop to keep going until the condition of running is False
while running == True:
    
    # First question of all 4 options
    questions = input("Choose a number option: ")
    print("")
    
    # Condition to choose option 1
    if questions == "1":
        print("Great! Let's print a poem.")
        print("")
        print(poem)
                
    # Condition to choose option 2    
    elif questions == "2":
        print("Great! Let's do some numbers.")
        numbers()
        
        
    # Condition to choose option 3    
    elif questions == "3":
        print("Great! Let's create a file.")
        
        x = open("file.txt", "x")
        print("A file has been created.")
        x.close()
    
    # Condition to choose option 4
    elif questions == "4":
        print("")
        print("Goodbye!")
        running = False
        
    # Condition if the user inputs a different number or character    
    else:
        print("Please only choose a number between 1 and 4")

The next stage of the project tries to develop the virtual assistant into a more complex application. It also aims to improve code readability and efficiency through new Python knowledge.

All this will be possible after completing Python Intermediate Tutorials.


Next: Python Performance