Python Intermediate Project Tutorial
Project of Python Intermediate Tutorials
This is a final project of the Python intermediate tutorial section. It continues the previous Python basics project. As such, it aims to improve the project into more complex program. In addition, the intermediate project focuses on implementing more efficient code and better readability with the new information from the intermediate Python tutorials.
This page covers only aspects of what has been taught in the intermediate section of the Python coding tutorials.
Full details of the project and code up to this stage, here. It is vital for the user to go through the first part of the project, in order to follow and comprehend the operations behind the virtual assistant.
Project Virtual Assistant
In this project tutorial, the virtual assistant application aims to improve its code readability and functionality.
The pervious 4 options (action) of the VA will now reduce to 3. But instead, 2 of the actions will change to more complex chunks of code.
The 3 actions are:
- Guess a number
- Calculation
- Create a file
The full code is at the bottom of the page.
Welcome message
The welcome message slightly changes, and more particularly the relevant option information.
The main modification is the empty string “print()” statements. To make the code more readable, the escape new line “\n” character is used.
# Welcome message print("Hello! I am your virtual assistant, and I am here to help you.\n") print("There are 3 options to choose from. Please choose the relevant option you would like help with.\n") print("1. Guess a number.") print("2. Calculation.") print("3. Create a file.\n")
Back menu option
The program introduces an option for going back. This action also works as an exit of the program, hence the reduction of the 4th option “exit program”.
The back menu option operates through a function, with one parameter. The parameter associates with the relevant question argument when the function is called.
Without redefining the variable “running” as global, the program cannot recognise it as the value change from True to False occurs in the local scope of the function. And as such, the variable outside the function does not really change its value.
Note that when only the question switch goes from True to False, the specific question action turns off but the main while loop keeps executing. This is the operation behind the back menu.
# Back menu function with 1 parameter def back_menu(q): back_menu = input("\nWould you like to return to the main menu?: ") if back_menu == "yes": # False to stop q1 loop and go back to main loop q = False print("\n1. Guess a number.") print("2. Calculation.") print("3. Create a file.") elif back_menu == "no": print("\nGoodbye!") # False to stop q1 loop and main loop (program) q = False global running running = False else: print("Please choose yes or no.")
Each option
Just like before, this section focuses on the development of each option (action) of the VA. Or more particularly in this instance, on the changes of each one.
There are no other modifications to the main structure of the program. The while loop that runs the application stays the same.
As mentioned above, the options change from 4 to 3. The first and second actions change completely. The third stays the same, with some improvements.
Guess a number (option 1)
The first action changes from “print a poem” to “guess a number”. This is an easy task, as it does not use a random number generator.
The secret number is permanent and defined in advance.
Question 1 introduces nested loops and conditions as well as a switch for the specific action. This allows the user to keep guessing, until the input reaches the correct number.
Without the break statement after the function of “back_menu()”, the program executes the input “sel_num” again in the case of a successful secret number. This is due to the function of “back_menu()” being inside the if statement, and not on the level of the while loop.
The break statement only applies in option 1 within this project instance.
# Condition to choose option 1 if questions == "1": print("Great! Let's guess a number.") # Setting an on/off switch of the first question while loop q1 = True while q1 == True: # Assign secret number 7 number = "7" sel_num = input("Please select a number between 1 and 10: ") if sel_num == "7": print("\nCongrants! The secret number is 7.") # Back menu option back_menu(q1) break else: print("Not the right number.")
Calculation (option 2)
The second action also changes, from “print the lucky numbers” to “calculation”. The option calculations offers the user to input 2 numbers and perform a simple calculation.
This is similar to a very basic calculator.
The second option of calculation also uses nested loops and conditionals, together its own switch.
There are multiple switches inside the conditional statements. The reason behind this is to stop the loop executing endlessly.
# Condition to choose option 2 elif questions == "2": print("Great! Let's do some calculations.") # Setting an on/off switch of the second question while loop q2 = True while q2 == True: print("Please select 2 numbers.") # Casting to force number input for calculation num1 = int(input("First number: ")) num2 = int(input("Second number: ")) operator = input("Please select the operator: ") if operator == "+": print(num1 + num2) # False to stop q2 loop q2 = False elif operator == "-": print(num1 - num2) # False to stop q2 loop q2 = False elif operator == "*": print(num1 * num2) # False to stop q2 loop q2 = False elif operator == "/": print(num1 / num2) # False to stop q2 loop q2 = False else: print("\nPlease select: (+) (-) (*) (/)") # Back menu option back_menu(q2)
Create a file (options 3)
The action of creating a file does not change to a different option. Instead, in this scenario it improves.
The modification happens through the addition of a while loop and a switch. This prevents the previous issue of output error in the case of an already existing file.
In addition, the program implements a “try/except” to work with the while loop.
There are two switches inside the try and except statements. The reason behind this is to stop the loop executing endlessly.
# Condition to choose option 3 elif questions == "3": print("Great! Let's create a file.") # Setting an on/off switch of the third question while loop q3 = True while q3 == True: # Try/except creating file try: x = open("file.txt", "x") print("A file has been created.") x.close() # False to stop q3 loop q3 = False except: print("Something went wrong! The file may already exist.") # False to stop q3 loop q3 = False # Back menu option back_menu(q3)
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 example, when the application runs with the second action (calculation), it requires input of numbers. But if instead the input is anything else, the application outputs an error. Furthermore, the back menu function does not recognise anything else than lowercase yes and no. For instance, if the input is Yes (capital Y), the VA produces “Please choose yes or no”.
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.\n") print("There are 3 options to choose from. Please choose the relevant option you would like help with.\n") print("1. Guess a number.") print("2. Calculation.") print("3. Create a file.\n") # Back menu function with 1 parameter def back_menu(q): back_menu = input("\nWould you like to return to the main menu?: ") if back_menu == "yes": # False to stop q1 loop and go back to main loop q = False print("\n1. Guess a number.") print("2. Calculation.") print("3. Create a file.") elif back_menu == "no": print("\nGoodbye!") # False to stop q1 loop and main loop (program) q = False global running running = False else: print("Please choose yes or no.") # 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 3 options questions = input("Choose a number option: ") print("") # Condition to choose option 1 if questions == "1": print("Great! Let's guess a number.") # Setting an on/off switch of the first question while loop q1 = True while q1 == True: # Assign secret number 7 number = "7" sel_num = input("Please select a number between 1 and 10: ") if sel_num == "7": print("\nCongrants! The secret number is 7.") # Back menu option back_menu(q1) break else: print("Not the right number.") # Condition to choose option 2 elif questions == "2": print("Great! Let's do some calculations.") # Setting an on/off switch of the second question while loop q2 = True while q2 == True: print("Please select 2 numbers.") # Casting to force number input for calculation num1 = int(input("First number: ")) num2 = int(input("Second number: ")) operator = input("Please select the operator: ") if operator == "+": print(num1 + num2) # False to stop q2 loop q2 = False elif operator == "-": print(num1 - num2) # False to stop q2 loop q2 = False elif operator == "*": print(num1 * num2) # False to stop q2 loop q2 = False elif operator == "/": print(num1 / num2) # False to stop q2 loop q2 = False else: print("\nPlease select: (+) (-) (*) (/)") # Back menu option back_menu(q2) # Condition to choose option 3 elif questions == "3": print("Great! Let's create a file.") # Setting an on/off switch of the third question while loop q3 = True while q3 == True: # Try/except creating file try: x = open("file.txt", "x") print("A file has been created.") x.close() # False to stop q3 loop q3 = False except: print("Something went wrong! The file may already exist.") # False to stop q3 loop q3 = False # Back menu option back_menu(q3) else: print("Please only choose a number between 1 and 3")
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.
Next: Python String Formatting