Mini project — A text menu

Lesson 24 of 26 · Basic Python

In this project we will make a text menu that offers the user several options. This way of working is also used in MicroPython programs on microcontrollers.


The idea of the project


  • The program keeps running in a while loop.
  • The user chooses an option.
  • The program reacts depending on the choice.

Example code



while True:
    print("1 - Print a message")
    print("2 - Add numbers")
    print("0 - Exit")

    choice = input("Choose an option: ")

    if choice == "1":
        print("Hello from the menu!")
    elif choice == "2":
        a = int(input("Enter the first number: "))
        b = int(input("Enter the second number: "))
        print("The sum is:", a + b)
    elif choice == "0":
        print("End of the program.")
        break
    else:
        print("Unknown option.")

Why is this important?


This is an example of working with states. In MicroPython the same principle is used for buttons, LEDs and sensors.