Python Forum

Full Version: Looped
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, i am trying to get the user to enter from a given set of options. For that i am using different functions like this:
def decide_input (element):
    string_input_list = ['Batch',  'Date', 'Village', 'Case Sheet', 'Name', 'Gender', 'Others']
    if element in string_input_list:
        data = input(element + ': ' ).title()
        if data == 'Exit':
            print('Exiting...')
            starter()
        if 'cchange' not in data:
            pass
    else:
        data = input(element + ' (Enter number): ' )
        if data == 'exit':
            print('Exiting..')
            starter()
        elif 'cchange' not in data:
            try:
                data = int(data)
            except:
                print ('Please enter a number')                                
                decide_input(element)
    print ('-'*88)
    return data

def check_bounds(element, lower_bound=0, upper_bound=0, options_list=[]):
    new_element = element
    options = options_list
    input_value = decide_input(element)
    if options_list != []:
        if input_value in options_list:
            return input_value
        else:
            print ('Please select from the listed options')
            del input_value
            check_bounds(element=new_element, options_list=options)
            return input_value
    elif input_value.isdigit() and input_value > lower_bound and input_value < upper_bound:
        return input_value
    else:
        print('Kindly enter an input between ' + str(lower_bound) and str(upper_bound))
        check_bounds(element, lower_bound, upper_bound)
        return input_value

print('Use the following code:\n A - Ramanathapuram\n B - Thondamanatham \n C - Tuthipet \n D - Piliarkuppam \n E - NSA')
data = check_bounds('Village', options_list = ['A', 'B', 'C', 'D', 'E'])
print('you have entered ' + data)
The loop stops when the user enters a valid option, but it outputs the first entered value. How can i fix that?
The problem is that you don't have a loop; you have recursion. On line 34, you make a second call to check_bounds() so the user can try again. This is called recursion and it adds to the stack.

To implement a loop, you need to use the keywords "for" or "while". Here's a possible solution:

def decide_input (element):
    string_input_list = ['Batch',  'Date', 'Village', 'Case Sheet', 'Name', 'Gender', 'Others']
    if element in string_input_list:
        data = input(element + ': ' ).title()
        if data == 'Exit':
            print('Exiting...')
            starter()
        if 'cchange' not in data:
            pass
    else:
        data = input(element + ' (Enter number): ' )
        if data == 'exit':
            print('Exiting..')
            starter()
        elif 'cchange' not in data:
            try:
                data = int(data)
            except:
                print ('Please enter a number')                                
                decide_input(element)
    print ('-'*88)
    return data
 
def check_bounds(element, lower_bound=0, upper_bound=0, options_list=[]):
    new_element = element
    options = options_list
    input_value = decide_input(element)
    while input_value not in options_list:
        print(
            "Invalid option. Please enter one of the following: {}".format(
                ", ".join(options_list)
            )
        )
        input_value = decide_input(element)

    if input_value.isdigit() and input_value > lower_bound and input_value < upper_bound:
        return input_value
    else:
        print('Kindly enter an input between ' + str(lower_bound) and str(upper_bound))
        check_bounds(element, lower_bound, upper_bound)
        return input_value
 
print('Use the following code:\n A - Ramanathapuram\n B - Thondamanatham \n C - Tuthipet \n D - Piliarkuppam \n E - NSA')
data = check_bounds('Village', options_list = ['A', 'B', 'C', 'D', 'E'])
print('you have entered ' + data)
You're using recursion, not a loop. The problem is that you are not passing the value up the recursion chain. Line 20 would need to be data = decide_input(element).

I think it would be much better to do this as a loop:

while True:
    data = input(question)
    if not valid(question):
        print(error_message)
    else:
        break
return data
That's just an outline, you would need to adapt it to your code. But that sort of format is how I always deal with this issue.