-1

I have the script where first the user is given a menu and then based on menu options the script runs.

def menu():
 descriptions = ('SetupFeeDescr', 'OveruseFeeDescr', 'RecurrFeeDescr', 'CancellationFeeDescr')
 planIds = input('Specify Plan Id. Multiple Ids seperated by comma: ')
 print('\n Which description would you like to update? \n')
 for i,j in enumerate(descriptions):
  print(i+1,"-",j)
 print("Q - Quit")
 user_input = input('Enter your selection: ')
 if user_input == '1':
    descr = descriptions[0]
 if user_input == '2':
    descr = descriptions[1]
 if user_input == '3':
    descr = descriptions[2]
 if user_input == '4':
    descr = descriptions[3]
 if user_input.lower() == 'q':
    return
 return(descr, planIds)

How do I make my 'main' loop forever until 'q' is given via menu?

if __name__ == '__main__':
 prefixes = get_prefix()
 descr, planIds = menu()
 data, old = get_plan_rates(planIds, prefixes, descr)
 replace_content(data, old, descr)

I don't think it's a duplicate of that other thread because I'm trying to loop over a definition indefinetly, and the script executes 4 defs.

oj43085
  • 115
  • 2
  • 11

1 Answers1

0

Just make an infinite while True loop and ask user input inside. If the input is "q", you call break and the loop ends. For you code it would be something like this:

while True:
    if user_input == '1':
        descr = descriptions[0]
    if user_input == '2':
        descr = descriptions[1]
    #....
    if user_input == 'q':
        break

Note: in each iteration you would overwrite the content of the descr variable. If you want to accumulate multiple choices, you have to use a data structure that can accomodate many values, like a list. This would then be:

descr = []
while True:
    if user_input == '1':
        descr.append(descriptions[0])
    if user_input == '2':
        descr.append(descriptions[1])
    #....
    if user_input == 'q':
        break

Then you pack everything into your menu() function, which at the end returns the descr list.

Update

If you want to call the menu() function itself again and again until user inputs 'q', then you do it like this:

if __name__ == '__main__':
    while True:
        if main() == "stop":
            break

def main():
    prefixes = get_prefix()
    result = menu()
    if result is None:
        return "stop"
    descr, planIds = result
    data, old = get_plan_rates(planIds, prefixes, descr)
    replace_content(data, old, descr)

Each time you call menu(), it returns either a pair of values, or, if the user input is "q", it returns None. Then you detect this condition and stop the script.

R Kiselev
  • 1,124
  • 1
  • 9
  • 18
  • But that's only true if my entire program is running in menu() , but menu() is returning data passed onto 2 other definitions in 'main' so after 'replace_content' it should go back to 'menu' unless it's a 'q' – oj43085 Sep 19 '17 at 08:08
  • oh, I see, i misunderstood the question. wait... – R Kiselev Sep 19 '17 at 08:09