Python Lesson 12 – Your First Program

In this tutorial, we will build our first program using the concepts related to functions that we have learned so far. To be more precise, we want to build a program that performs mathematical operations (addition, subtraction, multiplication, division, exponentiation) based on the user’s choice and inputs (i.e., the two values). The application must display a menu to the users allowing them to make a choice.

Our application consists of exactly five funcionalities, each corresponding to one of the five mathematical operations. Therefore, we can consider converting each functionality into a function. Functions are a great way to organize your code.


def addition(x, y):
    pass

def subtraction(x, y):
    pass

def multiplication(x, y):
    pass

def division(x, y):
    pass

def exponentiation(x, y):
    pass

Each function accepts two parameters (i.e., two numbers). In fact, we want our program to work with any input provided by the user. Clearly, each function must be implemented.


def addition(x, y):
    print(x + y)

def subtraction(x, y):
    print(x - y)

def multiplication(x, y):
    print(x * y)

def division(x, y):
    print(x / y)

def exponentiation(x, y):
    print(x ** y)

In the code above, each function has been correctly implemented and performs a specific operation.

Now, we want to build the menu to be displayed to the user. After that, the program should:

  • Allow the user to choose which operation to perform
  • Ask the user for two values to be used in the operations

# ... copy the five functions from above

def menu():
    print("Menu: ")
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Exponentiation")

menu() # call the function

# Output:
# Menu:
# 1. Addition
# 2. Subtraction
# ...

Basically, the menu() function serves as a menu, displaying various options to the user. Now, we need to prompt the user to choose an operation.


# copy this line below the last line of the last example

user_input = input("Which operation do you want to perform? ")

This way, the user has the opportunity to choose an operation. However, we need to validate the user’s input, as only the numbers from 1 to 5 (both inclusive) are allowed. Therefore, until the user enters a number between 1 and 5 inclusive, the program cannot proceed. The word until suggests us of using a while loop.


# copy the code above ...

menu() # call the function to display the menu to the user

user_input = input("Which operation do you want to perform?: ") # ask the user for the operation

valid_answers = ['1', '2', '3', '4', '5']

The valid_answers variable at line 7 represents the valid choices for the user. Note that when the user enters a choice, it is read as a string (not an integer).


while not user_input in valid_answers:
    print("Please enter a valid choice. \n")
    menu()
    user_input = input("Which operation do you want to perform? ")

As long as the user enters an invalid choice, the menu is displayed again, and the user must enter a new input.
Let’s recap the code we’ve built so far.


def addition(x, y):
    print(x + y)

def subtraction(x, y):
    print(x - y)

def multiplication(x, y):
    print(x * y)

def division(x, y):
    if y == 0:
        print("Math error: division by zero.")
    else:
        print(x / y)

def exponentiation(x, y):
    print(x ** y)
def menu():
    print("Menu: ")
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Exponentiation")
    print("\n")

menu() # call the function
user_input = input("Which operation do you want to perform? ")
while not user_input in ['1', '2', '3', '4', '5']:
    print("Please enter a valid choice. \n")
    menu()
    user_input = input("Which operation do you want to perform? ")

Now, when the user enters a valid answer, we can proceed by asking for the two numbers.


# ... copy the entire code above

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")

Clearly, both num1 and num2 must be integers in order to perform mathematical operations. We have to check it.


# ... copy the code above

while not num1.isdigit() or not num2.isdigit():
    print("\n") # new empty line for clarity
    print("Please enter valid numbers. \n")
    num1 = input("Enter your first number: ")
    num2 = input("Enter your second number: ")

A while loop is helpful here as well. As long as the user enters invalid inputs (inon-digit inputs), the user must enter new ones.
Therefore, both num1 and num2 are reassigned.

At this point, we can perform the type conversion on both num1 and num2 to integers.


# ... copy the code above

num1 = int(num1)
num2 = int(num2)

To be more precise, we need to analyze the case of division. Specifically, we must consider that division by zero is not allowed. Therefore, refer to the beginning of the code, specifically to the divison function.


# modify the function

def division(x, y):
    if y == 0:
         print("Math error: division by zero.")§
    else:
         print(x / y)

When y is equal to 0, division is not allowed (the divisor must be different from 0). For this reason, an error message is displayed to the user in such cases.

Finally, we need to perform the operation required by the user, using the numbers provided.


# ... copy the code above

if user_input == '1':
    addition(num1, num2)
elif user_input == '2':
    subtraction(num1, num2)
elif user_input == '3':
    multiplication(num1, num2)
elif user_input == '4':
    division(num1, num2)
else:
    exponentiation(num1, num2)

Basically, we call the operation related to the user’s choice.

Here’s the full code:


def addition(x, y):
    print(x + y)

def subtraction(x, y):
    print(x - y)

def multiplication(x, y):
    print(x * y)

def division(x, y):
    if y == 0:
        print("Math error: division by zero.")
    else:
        print(x / y)

def exponentiation(x, y):
    print(x ** y)

def menu():
    print("Menu: ")
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Exponentiation")
    print("\n")

menu() # call the function
user_input = input("Which operation do you want to perform? ")

while not user_input in ['1', '2', '3', '4', '5']:
    print("Please enter a valid choice. \n")
    menu()
    user_input = input("Which operation do you want to perform? ")

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")

while not num1.isdigit() or not num2.isdigit():
    print("\n")
    print("Please enter valid numbers. \n")
    num1 = input("Enter your first number: ")
    num2 = input("Enter your second number: ")

num1 = int(num1)
num2 = int(num2)

if user_input == '1':
    addition(num1, num2)
elif user_input == '2':
    subtraction(num1, num2)
elif user_input == '3':
    multiplication(num1, num2)
elif user_input == '4':    
    division(num1, num2)
else:
    exponentiation(num1, num2)