Reload a list using append

Hi all,

I’m using append to create multiple options to choose to run. I put print and it prints the list. What I’m trying to do is reload the list itself after the option has been chosen. Is there any way to do this?

Thanks

Can you share the relevant code? Unclear what you mean by ‘reload’ but perhaps the code will make it clearer

Oh yeah sorry! I wrote the question in a hurray before end of the day. Here is a paraphrase of the code:

\\
mylist =
mylist.append("Main Banner: ")
mylist.append(“1. Option 1”)
mylist.append(“2. Optiion 2”)

print(mylist[0])
print(mylist[1])
print(mylist[2])
print(“”)

Option = input("Choose option above: ")

if Option == 1:

////

If the user chooses option 1, the actual command runs and finishes with exit code zero. But after that nothing, so I’m trying to make it reload the menu itself, hopefully.

Thanks

You could add a while loop around all this, so it keeps looping until the user selects the “Exit” option.

Hi,

formatting your code improves readability.

You can try something like the following script. You define the functions that you want to run per the available menu options. You then create a dictionary where you associate each function to a corresponding string menu option.

As stated by the previous user, you enter the code inside a while loop. This will keep displaying the menu option after every selection. If the user wants to exit, they can just enter the associated string to exit. I have written it with the string input Leave for simplicity, but you can change it to whatever you’d like.

# Define functions for the menu options
def opt_1():
    print('\n* Running option 1 ... *')

def opt_2():
    print('\n* Running option 2 ... *')

# Associate menu option with corresponding function 
mylist = {'Option 1': opt_1, 'Option 2': opt_2}

while True:

    # Options list:
    print('\nMain Banner')
    print('1. Option 1')
    print('2. Option 2\n')

    Option = input("Choose option above: ")

    if Option == 'Leave':
        print('\nExiting the program ...')
        break
    else:
        mylist[Option]()

By the way, if you would like to add additional menu options to the menu, you can do so like this:

# Define new function to add
def opt_3():
    print('\n* Running option 3 ... *')

mylist['Option 3'] = opt_3  # Add additional option to the menu

Thank you for the input. I’m going to go through it. I don’t do much Python in my role, and it’s hard to make time each day, though I want to learn it better. Still glad I found this community. I’ll update soon.

I did what you suggested and it worked! I was missing a command, I think I saw the while True before but didn’t try it. Thank You. Working on a basic desktop tool but in python. Starting with nslookup and testing port connectivity to the destination IP. Once I get nslookup working it’s a start. Did the same type of project in powershell so working to create in Python. Stuff is not easy.

Hang in there. You’re doing just fine. The more you learn, the more interesting and fun it gets as you can create more interested projects. By the way, I forgot to include the Leave option as part of a menu option in the printed list. You should include that option as well; i.e., print("3. Enter 'Leave' to exit."), as an example.

Thank you I appreciate that. I got into the code realm if you will, with Powershell. I’m no expert in that either but I didn’t want to be bound to only that. Usually my questions to the community are me trying to take the PS picture and put it into Python is that makes sense. Thanks again!