Dustin Sen
Dustin Sen

Reputation: 35

Create a variable to save all data from user input

I am writing a small console program to get values from user and iterate over them and show it back to the user. But I should keep all the values which I am getting from a user in one variable. Here is what I could create:

def get_options():
    print('============= Intranet App ==============')
    print('Please, choose the activity. Enter the corresponding number.')
    print('1. Create user')
    print('2. Notify users')
    print('3. Quit app')

    a = int(input('Option: '))

    all_users = []
    print('In variable you have these values', all_users)
    while True:
        if a == 1:
            print('Please enter the user type. (i.e., student, lecturer, management)')
            global user_type
            user_type = input('Enter user roles:')
            user_list = user_type.split()

        if user_type:
            print('Exiting program..')
            get_options()
        print(user_type, 'is created')
        all_users += user_list
        print(all_users)

But when new recursion starts values from all_users disappear. What can I do to keep all values in one variable and these values never should be deleted?

Upvotes: 0

Views: 128

Answers (2)

Endyd
Endyd

Reputation: 1279

If you add a keyword argument and pass the list to the layers of recursion below, you can persist the all_users list.

def get_options(all_users=None):
    if all_users is None:
        all_users = []
# later in the func
    get_options(all_users=all_users)

Upvotes: 1

shivankgtm
shivankgtm

Reputation: 1242

You just need to make your all users list global instead of local for get_options. define the all_users outside your get_options function.

Upvotes: 0

Related Questions