在 Python 中的一系列函数后无法 return 变量
Failure to return variable after series of functions in Python
我正在尝试 return 一个通过一系列函数操作的变量 ('brand')。如果我通过第一个函数 (brandSelector) 找到变量和数组之间的交集,则变量是 returned。但是,如果在第一个失败的情况下选择了次要子路由,则变量会到达 brandDetectionFailure(),但它不会 return 开始(Main() 函数)。我试过使用 'global brand' 但它没有用。我将不胜感激关于强制变量 returned 到开始的任何建议,而不是在主函数打印回时接收 None。
主程序流控注意事项:
Returns 跟随 brandSelector() --> brandDetectionFailure()(来自异常处理程序)--> defineBrand()
#Import Modules
import sys, warnings, string
#Define Globals
brands = ["apple", "android", "windows"]
brand = None
def init():
#Define
#warnings.filterwarnings("ignore") #Disable for debuggings
#Main Menu
print("--Welcome to Troubleshooting Applet--")
print("In your query please include the brand of your device and the problem / symptom of your current issue. \n")
def Main():
init()
query = input("Enter your query: ").lower()
brand = brandSelector(query) #Returns here after following brandSelector --> brandDetectionFailure (from exception handler) --> defineBrand
print(brand)
#secondaryMain(query)
print("END")
def secondaryMain(query):
#Push required variables to next stage
print(query)
##START Brand Selection Route
def brandSelector(query):
try:
#Format Brands Query
brand = set(brands).intersection(query.split())
brand = ', '.join(brand)
#Check Condition After Setting
int_cond = confirmIntersection(brand)
if int_cond == False:
raise NameError("No Intersection")
#brandDetectionFailure()
return brand
except NameError:
print("\nNo Intersection found between query defined brand and brands array\n")
brandDetectionFailure()
def confirmIntersection(brand):
if brand in brands:
return True
else:
return False
##END Brand Selection Route
##START Brand Selection Route | SUB: Failed Selection
def brandDetectionFailure():
print("-Brand could not be found-")
print("Query still stored for your issue")
if userConfirm("Would you like to redefine a brand and continue?"):
defineBrand()
else:
end()
def defineBrand():
brand = input("Enter your device's brand: ")
int_cond = confirmIntersection(brand)
if int_cond == False:
if userConfirm("Try again?"):
defineBrand()
else:
end()
else:
print("End of Sub Route") #STILL NEEDS WORK
return brand
##END Brand Selection Route | SUB: Failed Selection
##START Generic Functions
def userConfirm(question):
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
else:
return userConfirm("Please confirm using yes or no")
def end():
print("Have a good day. Bye.")
sys.exit()
##END Generic Functions
if __name__ == '__main__':
Main()
谢谢,
山姆
感谢 Barmar (https://whosebug.com/users/1491895/barmar),
我遵循了他的解决方案:
"brandDetectionFailure doesn't have a return statement. And your except block in brandSelector doesn't have a return statement. So nothing gets returned in that case."
随后对
进行了澄清
"It should be return brandDetectionFailure(). And brandDetectionFailure needs to have return defineBrand()."
综上所述,"Every function needs to return something."
我正在尝试 return 一个通过一系列函数操作的变量 ('brand')。如果我通过第一个函数 (brandSelector) 找到变量和数组之间的交集,则变量是 returned。但是,如果在第一个失败的情况下选择了次要子路由,则变量会到达 brandDetectionFailure(),但它不会 return 开始(Main() 函数)。我试过使用 'global brand' 但它没有用。我将不胜感激关于强制变量 returned 到开始的任何建议,而不是在主函数打印回时接收 None。
主程序流控注意事项: Returns 跟随 brandSelector() --> brandDetectionFailure()(来自异常处理程序)--> defineBrand()
#Import Modules
import sys, warnings, string
#Define Globals
brands = ["apple", "android", "windows"]
brand = None
def init():
#Define
#warnings.filterwarnings("ignore") #Disable for debuggings
#Main Menu
print("--Welcome to Troubleshooting Applet--")
print("In your query please include the brand of your device and the problem / symptom of your current issue. \n")
def Main():
init()
query = input("Enter your query: ").lower()
brand = brandSelector(query) #Returns here after following brandSelector --> brandDetectionFailure (from exception handler) --> defineBrand
print(brand)
#secondaryMain(query)
print("END")
def secondaryMain(query):
#Push required variables to next stage
print(query)
##START Brand Selection Route
def brandSelector(query):
try:
#Format Brands Query
brand = set(brands).intersection(query.split())
brand = ', '.join(brand)
#Check Condition After Setting
int_cond = confirmIntersection(brand)
if int_cond == False:
raise NameError("No Intersection")
#brandDetectionFailure()
return brand
except NameError:
print("\nNo Intersection found between query defined brand and brands array\n")
brandDetectionFailure()
def confirmIntersection(brand):
if brand in brands:
return True
else:
return False
##END Brand Selection Route
##START Brand Selection Route | SUB: Failed Selection
def brandDetectionFailure():
print("-Brand could not be found-")
print("Query still stored for your issue")
if userConfirm("Would you like to redefine a brand and continue?"):
defineBrand()
else:
end()
def defineBrand():
brand = input("Enter your device's brand: ")
int_cond = confirmIntersection(brand)
if int_cond == False:
if userConfirm("Try again?"):
defineBrand()
else:
end()
else:
print("End of Sub Route") #STILL NEEDS WORK
return brand
##END Brand Selection Route | SUB: Failed Selection
##START Generic Functions
def userConfirm(question):
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
else:
return userConfirm("Please confirm using yes or no")
def end():
print("Have a good day. Bye.")
sys.exit()
##END Generic Functions
if __name__ == '__main__':
Main()
谢谢,
山姆
感谢 Barmar (https://whosebug.com/users/1491895/barmar),
我遵循了他的解决方案:
"brandDetectionFailure doesn't have a return statement. And your except block in brandSelector doesn't have a return statement. So nothing gets returned in that case."
随后对
进行了澄清"It should be return brandDetectionFailure(). And brandDetectionFailure needs to have return defineBrand()."
综上所述,"Every function needs to return something."