为什么我的另一个函数仍在运行,我打破了主函数中的循环

  • 本文关键字:函数 循环 另一个 运行 python
  • 更新时间 :
  • 英文 :


我使用的示例代码其主要思想是,当用户输入错误的输入时,它将返回一个菜单选项,让用户返回到mainmenu或继续但是,当我想输入数据并选择返回main_menu并尝试终止它时,TenantID函数仍在运行

def TenantID():              
while True:       
error_list = []      
tenant_id = str(input("1.Enter your Tenant ID: "))
if ValidateTenantID(tenant_id, error_list):
print(*error_list)
if MenuChoice() is True:
menu()
else:
continue
else:
break
return tenant_id

def MenuChoice(): 
Choice = input("x1B[1m" + "Press M return to main menu or Any key 
to continue: " + 'x1B[0m')
if Choice in ["M", "m"]:
return True
else:
return False
def ValidateTenantID(tenant_id, error_list):
returnValue = CheckLength(tenant_id)
if returnValue is True:
returnValue = CheckTenantID(tenant_id)
if returnValue is True:
returnValue = CheckDataIfExist(tenant_id)
if returnValue is not True:
error_list.append(returnValue)
else:
error_list.append(returnValue)
else:
error_list.append(returnValue)
return error_list

问题

如果您想从菜单功能中终止,则从中终止return和/或停止运行它的循环。

例如,

running = True
def menu():
print('options...')
choice = input('> ')
if choice == '5':
global running
running = False
return  # completely stop this function from doing other things
elif choice == '1':
tid = TenantID()
def TenantID(): 
valid = False             
while not valid:       
error_list = []
tenant_id = input("1.Enter your Tenant ID: ")
valid = ValidateTenantID(tenant_id, error_list)
print(*error_list)
if MenuChoice():
break
if not valid:
return None
else:
return tenant_id
while running:  # can be changed from the menu function via global variable
menu()
print('bye')

最新更新