while 循环没有终止 python
while loop not terminating python
我正在编写一个交互式程序,在 select 选择选项后显示 y/n。如果在开始时我按 s 或 c while 循环终止。如果我 select some no first time "say 5" 第 5 个选项变为 y 但之后如果我按 "s" 或 "c" 它显示第 5 个条目为 "n" 并且程序没有退出。不知道出了什么问题。初学者问题。(替代解决方案也很受欢迎,也不能导入其他包)
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)
问题在于,对于输入数字的情况,您会再次递归调用相同的函数。如果/当该调用退出时,执行将在此调用中继续,返回到 while 循环。
因为你有一个while循环,你不需要递归调用。如有必要,循环将处理重复的事情。
我正在编写一个交互式程序,在 select 选择选项后显示 y/n。如果在开始时我按 s 或 c while 循环终止。如果我 select some no first time "say 5" 第 5 个选项变为 y 但之后如果我按 "s" 或 "c" 它显示第 5 个条目为 "n" 并且程序没有退出。不知道出了什么问题。初学者问题。(替代解决方案也很受欢迎,也不能导入其他包)
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)
问题在于,对于输入数字的情况,您会再次递归调用相同的函数。如果/当该调用退出时,执行将在此调用中继续,返回到 while 循环。
因为你有一个while循环,你不需要递归调用。如有必要,循环将处理重复的事情。