'Trying to make a blank list that is mutable on python

I need to be able to add, delete, edit, and recall transactions in a list.

I opened a while loop, set the constants and defined an empty list. I then proceeded to create the menuing through if statements. Everything seems to work except I am unable to add to the list and then recall all added values.

This is what I have so far:

while True:

    addTransaction = "1"
    deleteTransaction = "2"
    editTransaction = "3"
    displayTransactions = "4"
    exit = "5"
    transactions = []
    
    menu = input("**********Main Menu***********\n1.  Add Transaction\n2.  Delete Transaction\n3.  Edit Transaction\n4.  Display Transactions\n5.  Exit the Program\n\nChoose a menu option from 1-4 or 5 to exit the program: ")
    if menu != "5":
        if menu == "1":
            newTransaction = (input("Enter new transaction: "))
            transactions.append(newTransaction)
            print("Added")
        elif menu == "2":
            target = input("Edit transaction: ")
            if target in transactions:
                transactions.pop(transactions.index(target))
                print("Removed")
            else:
                print("Transaction not found.")
        elif menu == "3":
            transactions[input("Enter the transaction key to edit: ")] = input("Enter the new transaction key: ")
        elif menu == "4":
            print("******************\n** Transactions **\n" + str(transactions) + "\n******************")
        else:
            print("Invalid input.")
    else:
        break

I am brand new to Python, trying to figure out how to use all of these functions



Solution 1:[1]

You just need to make sure transactions is not in the while loop. Because it was being reset to [] every iteration.

addTransaction = "1"
deleteTransaction = "2"
editTransaction = "3"
displayTransactions = "4"
exit = "5"
transactions = []

while True:

    menu = input("**********Main Menu***********\n1.  Add Transaction\n2.  Delete Transaction\n3.  Edit Transaction\n4.  Display Transactions\n5.  Exit the Program\n\nChoose a menu option from 1-4 or 5 to exit the program: ")
    if menu != "5":
        if menu == "1":
            newTransaction = input("Enter new transaction: ")
            transactions.append(newTransaction)
            print("Added")
        elif menu == "2":
            target = input("Remove transaction: ")
            if target in transactions:
                # Use remove to remeve the first occurance of a value
                transactions.remove(target)
                print("Removed")
            else:
                print("Transaction not found.")
        elif menu == "3":
            transactions[input("Enter the transaction key to edit: ")] = input(
                "Enter the new transaction key: ")
        elif menu == "4":
            # Use join to join elements in a list
            print("******************\n** Transactions **\n" +
                  ", ".join(transactions) + "\n******************")
        else:
            print("Invalid input.")
    else:
        break

You may also want to experiment with match. Which is a Python switch case basically

menu = "1"

match menu:
    case "1":
        # Code here for when menu = 1
    case "2":
        # Code here for when menu = 2
    case _:
        # Code here for when menu != 1 or 2
        print("Error")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1