在Python中的一系列函数之后无法返回变量

时间:2016-08-19 23:24:18

标签: python function variables return-value

我试图返回一个通过一系列函数操作的变量('品牌')。如果我通过第一个函数(brandSelector)找到变量和数组之间的交集,则返回变量。但是,如果在第一个失败的情况下选择了辅助子路由,则变量到达brandDetectionFailure(),但是它不会返回start(Main()函数)。我尝试过使用“全球品牌”,但它没有用。我将不胜感激任何关于强制变量返回到开头的建议,而不是在主函数打印时接收无。

注意主程序的流量控制: 遵循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()

谢谢,

萨姆

1 个答案:

答案 0 :(得分:0)

感谢Barmar(https://stackoverflow.com/users/1491895/barmar),

我按照他的解决方案:

" brandDetectionFailure没有退货声明。而且,brandSelector中的except块没有返回语句。因此,在这种情况下不会返回任何内容。"

然后澄清

"应该返回brandDetectionFailure()。而且BrandDetectionFailure需要返回defineBrand()。"

总之,"每个功能都需要返回一些东西。"