Return to menu application button

import datetime

# Sample data records
sample_data = [
    {"name": "VM1", "type": "General Purpose", "amount of ram": 3.5, "num of data disk": 4, "max iops": 3200, "premium disk supported": True, "cost per month": 60.09, "last deployed": datetime.datetime(2023, 11, 12, 12, 32, 12), "setting size": "HD1_v2"},
    {"name": "VM2", "type": "General Purpose", "amount of ram": 512, "num of data disk": 32, "max iops": 80000, "premium disk supported": False, "cost per month": 4712.19, "last deployed": datetime.datetime(2023, 5, 12, 15, 23, 16), "setting size": "L64s_v3"}, 
    {"name": "VM3", "type": "Memory Optimized", "amount of ram": 256, "num of data disk": 32, "max iops": 1920, "premium disk supported": True, "cost per month": 312.86, "last deployed": datetime.datetime(2024, 3, 11, 9, 11, 2), "setting size": "D64as_v4"},
    {"name": "VM4", "type": "General Purpose", "amount of ram": 8, "num of data disk": 16, "max iops": 5600, "premium disk supported": True, "cost per month": 42.98, "last deployed": datetime.datetime(2023, 5, 12, 23, 55, 1), "setting size": "B2ms"}, 
    {"name": "VM5", "type": "GPU", "amount of ram": 64, "num of data disk": 4, "max iops": 12800, "premium disk supported": False, "cost per month": 32.56, "last deployed": datetime.datetime(2023, 11, 11, 18), "setting size": "B2ms"},
    {"name": "VM6", "type": "Memory Optimized", "amount of ram": 8, "num of data disk": 4, "max iops": 80000, "premium disk supported": True, "cost per month": 89.12, "last deployed": datetime.datetime(2023, 1, 12, 18), "setting size": "L64s_v3"},
    {"name": "VM7", "type": "GPU", "amount of ram": 448, "num of data disk": 16, "max iops": 3200, "premium disk supported": True, "cost per month": 43.21, "last deployed": datetime.datetime(2024, 1, 24, 19, 46, 58), "setting size": "E8-2s_v4"},
    {"name": "VM8", "type": "General Purpose", "amount of ram": 3.5, "num of data disk": 4, "max iops": 3200, "premium disk supported": True, "cost per month": 23.92, "last deployed": datetime.datetime(2024, 2, 23, 18), "setting size": "L64s_v3"},
]

def display_records(records):
    print("Sample Data Records:")
    for vm in records:
        print(f"Name: {vm['name']}, Type: {vm['type']}, RAM: {vm['amount of ram']} GB, Data Disks: {vm['num of data disk']}, IOPS: {vm['max iops']}, Premium Disk: {vm['premium disk supported']}, Cost/Month: £{vm['cost per month']:.2f}, Last Deployed: {vm['last deployed']}, Setting Size: {vm['setting size']}")
    print()

def add_vm(records):
    new_vm = {}
    new_vm['name'] = input("Enter the name of the new VM: ")
    new_vm['type'] = input("Enter the type of the new VM: ")
    new_vm['amount of ram'] = float(input("Enter the amount of RAM (GB) for the new VM: "))
    new_vm['num of data disk'] = int(input("Enter the number of data disks for the new VM: "))
    new_vm['max iops'] = int(input("Enter the maximum IOPS for the new VM: "))
    new_vm['premium disk supported'] = bool(input("Is premium disk supported for the new VM (True/False): ").lower() == 'true')
    new_vm['setting size'] = input("Enter the setting size for the new VM: ")

    # Add the new VM record to the sample data
    records.append(new_vm)
    print(f"VM '{new_vm['name']}' added successfully.")

def amend_vm(records):
    display_records(records)
    try:
        vm_number = int(input("Enter the number of the VM to amend: "))
        if 1 <= vm_number <= len(records):
            vm = records[vm_number - 1]
            print(f"Amending VM '{vm['name']}':")
            # Prompt user for changes (e.g., RAM, data disks, etc.)
            vm['type'] = (input(f"New Type[{vm['type']}]: "))
            vm['amount of ram'] = float(input(f"New RAM (GB) [{vm['amount of ram']}]: "))
            vm['num of data disk'] = int(input(f"New Data Disks [{vm['num of data disk']}]: "))
            vm['max iops'] = int(input(f"New IOPS [{vm['max iops']}]: "))
            vm['premium disk supported'] = bool(input(f"New Premium Disk Supported [{vm['premium disk supported']}]: "))
            vm['setting size'] = (input(f"New Setting Size [{vm['setting size']}]: "))
            print(f"VM '{vm['name']}' amended successfully.")
        else:
            print("Invalid VM number. Please select a valid VM.")
    except ValueError:
        print("Invalid input. Please enter a valid number.")

def delete_vm(records):
    display_records(records)
    try:
        vm_number = int(input("Enter the name of the VM to delete: "))
        if 1 <= vm_number <= len(records):
            deleted_vm = records.pop(vm_number - 1)
            print(f"VM '{deleted_vm['name']}' deleted successfully.")
        else:
            print("Invalid VM number. Please select a valid VM.")
    except ValueError:
        print("Invalid input. Please enter a valid number.")

def main():
    while True:
        print("Main Menu:")
        print("1. Display Virtual Machines")
        print("2. Add Virtual Machine")
        print("3. Amend an exsisting Virtual Machine")
        print("4. Delete Virtual Machine")
        print("5. Quit Application")
        choice = input("Enter: ")

        if choice == "1":
            display_records(sample_data)
        elif choice == "2":
            add_vm(sample_data)
        elif choice == "3":
            amend_vm(sample_data)
        elif choice == "4":
            delete_vm(sample_data)
        elif choice == "5":
            print("Thank you for using the application. Goodbye!")
            break
        else:
            print("Invalid choice. Please select a valid option.\n")

if __name__ == "__main__":
    main()

Please could someone advise how to get a return to the main menu present at any point/stage in the application which I have created above. my menu consists of having a list mentioned above of deleting, viewing, amending, qutitting the application and require an option at any stage Im in the application to return to this by selecting ‘m’ for example and have it project on the console at all times so the user is able to know.

Please read the pinned thread and edit the post to format the code properly (you will probably have to erase all the code, paste it in again, and then format it), so that we can properly see how it’s structured. Otherwise it’s impossible to give correct advice.

Code has been formatted, let me know if this is easier to understand.

It’s not clear to me what you mean by “at any stage”. Could you show an example of what happens when you try using the program - i.e., a few specific inputs you might try; what happens as a result; what you expect to happen next; and especially what does happen next, and how that is different?

When I try the program, I find that after using any given command, I come back to the main menu automatically (which is as I would expect from reading the code). I don’t understand how what you want, is different from that.

If you’re building an if/elif/else block in a function and you want a way to get back to __main__, add an option like:

elif vm_number == 'Q':
    return None

although you will have to structure your program to interpret the function not returning its “normal” output.

For example in the console when the user is entering detail for a new VM such as type, location, name etc…at any point I would like to have an option to return to the main menu. It does return after I’ve added, deleted or amended a VM. But whilst doing any of those actions I would like an option to return to the main menu for example.

Enter the VM you would like to delete?..
Return to main menu

Add Name…Add size etc…
Return to main menu - (this is where I want to have a main menu present at all times not only just completing an action)

What would you like the user to type in order to get this option?

What if that text would be valid input otherwise?

User types in ‘menu’ and then enter

Okay, and what if I want a VM that’s named menu?

User should enter following standards of VM name for this example if virtuals machines are named VM1…VM2… user must follow standards of naming convention. hence they should not call Vm ‘menu’.