Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looped
#1
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?
Reply
#2
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)
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Arrange my looped template data in rows JedBoyle 0 1,553 Feb-19-2020, 01:41 PM
Last Post: JedBoyle
  Unwanted delay between looped synth plays WolfeCreek 1 2,318 Aug-02-2018, 09:24 PM
Last Post: Vysero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020