0

i am writing an interactive program that displays y/n after selecting the option. if at begining itself i press s or c the while loop terminates. if i select some no first time "say 5" the 5th option turns to y but after that if i press "s" or "c" it shows the 5th entry as "n" and the program is not exited. not sure what's going wrong. beginner question.(alternate solution is also appretiated, can't import other packages as well)

import os,time
selected_list=[]
def px_filter_menu(num):
    os.system("clear")
    data = ["p1","p2","p3","p4","p5","p6","p7","p8","p9","p10","p11","p12","p13","p14","p15","p16","p17","p18","p19","p20","p21","p22","p23","p24","p25","p26"]

    print("============================================================================\n= Filter selection menu")
    print("============================================================================\n\n")

    gap =5

    for count , item in enumerate(data, 1):
        if count <10:
            if data[count-1] in selected_list:
                print("{0}.  {1} {2}".format(count,item.ljust(gap),"[y]"))
            else:
                print("{0}.  {1} {2}".format(count,item.ljust(gap),"[n]"))
        else:
            if data[count-1] in selected_list:
                print("{0}. {1} {2}".format(count,item.ljust(gap),"[y]"))
            else:
                print("{0}. {1} {2}".format(count,item.ljust(gap),"[n]"))

    print("\n\n[S] save and exit")
    print("\n\n[C] if you press C and wish to make changes please remove the PX and add again\n")

    input = raw_input("enter the desired number ")
    
    if len(input.strip()) ==0:
        print("first")
        time.sleep(2)
        px_filter_menu(-1)
       
    while True:
        if input.upper()=="S":
            print("5")
            time.sleep(2)
            break

        elif input.upper()=="C":
            del selected_list[:]
            #by default all protocol goes
            print("6")
            time.sleep(2)
            break

        elif input.isdigit() and data[int(input)-1] in selected_list:
            print(input)
            selected_list.remove(data[int(input)-1])
            print("3")
            time.sleep(2)
            px_filter_menu(-1)

        elif input.isdigit():
            selected_list.append(data[int(input)-1])
            print("4")
            time.sleep(2)
            px_filter_menu(-1)
        else:
            px_filter_menu(-1)
    return

px_filter_menu(-1)
6
  • use big 'S' and 'C', while you using .upper() in input Commented Sep 9, 2020 at 6:15
  • 1
    Where do you get a 2nd input? I only ever see you ask once .... you need to ask for a new input inside the wile loop to reset whats being choosen input = raw_input("enter the desired number ") Commented Sep 9, 2020 at 6:16
  • @ZarakiKenpachi that still does not termiate the loop if first some number is given and then s/c is given Commented Sep 9, 2020 at 6:16
  • 1
    There still are cases where you enter an infinite while loop, without ever updating your condition, and never encountering a break. That there’s a recursive call in there is irrelevant. Commented Sep 9, 2020 at 6:19
  • 2
    @sai - so what? You are inside a while True: you get an input that lets you recuse into antoher version of yourself getting a new input - if you now break you are back into the 1st while True and can not escape IT because you never reset its input. BTW the "design" is not what you want to do - use a straight loop - not a recursion. Commented Sep 9, 2020 at 6:20

1 Answer 1

3

The problem is that for the cases where a number is given as input, you recursively call the same function again. If / when that call exits, execution continues in this call, going back into the while loop.

Since you have a while loop, you do not need recursive calls. The loop will take care of things being repeated, if necessary.

Sign up to request clarification or add additional context in comments.

2 Comments

but then i wont be able to change the toggle to y/n when user selects it right
Well, that means you'll have to put the user input inside the while loop. You should either use recursion or a loop, not both. Mixing the two will lead to problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.