# Creating our user menu

Let's start coding our programming diary app! To begin, we'll create a folder called progdiary to hold our project files. Inside it, we'll create app.py to contain our main application logic. I've ended up with this structure:

- progdiary/
  | - app.py

My first steps when it comes to developing an user menu are always to define a few variables that I'll need.

For this application, we first of all need the main prompt:

menu = """Please select one of the following options:
1) Add new entry for today.
2) View entries.
3) Exit.

Your selection: """

I'll also define a variable for the initial welcome string. We only use this in one place, but defining a variable for it at the top of our file will help the code be more readable:

menu = """Please select one of the following options:
1) Add new entry for today.
2) View entries.
3) Exit.

Your selection: """
welcome = "Welcome to the programming diary!"

# Looping while asking the user for an option

The core part of our application involves asking the user over and over, while dealing with their input each time. Therefore we will need a loop:

user_input = input(menu)
while user_input != "3":
    # We'll deal with user input here...

    user_input = input(menu)

Structuring the loop in this way means that:

  • First of all we'll ask the user for their choice, outside of the loop.
  • If they didn't enter "3", we'll deal with their input.
  • We ask them for another piece of input.
  • We deal with it (exiting if they entered "3").
  • Repeat until they do exit.

# Using Python 3.8's Walrus Operator

In Python3.8 we got a new operator[1] that can simplify this loop:

 


while (user_input := input(menu)) != "3":
    # We'll deal with user input here...

The walrus operator, :=, can be read as "being equal.". So the while loop means "while user_input, being equal to the user's input, is not equal to 3".

This loop will automatically ask the user for new input each iteration, so we can remove that last line from earlier.

WARNING

It's important to place (user_input := input(menu)) inside brackets, as the walrus operator has less important order of precedence than the comparison operators. If you remove the brackets:

while user_input := input(menu) != "3":

That means "while user_input, being equal to the user's input not being 3, is truthy". This loop would run only if the user's input is not three, but the value of user_input would only ever be True or False, as it is assigned after the comparison is evaluated.

Confusing stuff, so just remember the brackets!

# Writing our user options

My next step is usually to map out the different user options to create the backbone of the application:


 
 
 
 



while (user_input := input(menu)) != "3":
    if user_input == "1":
        print("Adding...")
    elif user_input == "2":
        print("Viewing...")
    else:
        print("Invalid option, please try again!")

I'm printing "Adding..." and "Viewing..." because at the moment we're not ready to actually add or view entries (we don't even have a database set up!). This is good enough for now.

Later on, we will be adding code inside those if statements. But this lets us try out our application and make sure our logic works.

# Adding the initial welcome

We also must remember to print our welcome message. We must not do this inside the loop, as then we'd welcome users to our application every time they give us input!

Instead, we'll do this before the loop:









 









menu = """Please select one of the following options:
1) Add new entry for today.
2) View entries.
3) Exit.

Your selection: """
welcome = "Welcome to the programming diary!"

print(welcome)

while (user_input := input(menu)) != "3":
    if user_input == "1":
        print("Adding...")
    elif user_input == "2":
        print("Viewing...")
    else:
        print("Invalid option, please try again!")

And that's our complete code for the user menu!

Next up we're going to be adding functionality to keep track of entries. Only we won't be using a database just yet. First we'll develop our program logic by using a Python list as a simple database table.

That allows us to quickly develop our program without worrying about interacting with a third-party service (the database in this case).

Once we're happy with all our functions and how they work, we can read and write from the database instead of from a Python list. Changing that will be straightforward, and this way we only have to worry about one aspect of software development at a time.


  1. Assignment expressions in Python (Teclado) (opens new window) ↩︎