Python: Return value not found in function

Signaler
Messages postés
1
Date d'inscription
mardi 30 mars 2021
Statut
Membre
Dernière intervention
30 mars 2021
-
Salut @everyone!

Je galère un peu … Je ne suis pas un pro de python, je débogue un programme chatboot. J’ai mon algorithme, ma fonction build_answer effectue une recherche pour trouver "order_info" dans message, question, history, si après order_info n’a pas été trouvé, alors je voudrais un retour "Not found".

L’aide sera vraiment la bienvenue.

Voici où je suis et ma progression …





def build_answer(shop,order_info,message,questions,history,is_buffalo):

    questions_2 = ["info_not_found","none"]
    history_line = None

    # CASE 1: if the customer has already a question
    for j in range(len(history)):
        if (message["email"] in history[j]):
            if history[j][2] == "no": # Question and then customer_id
                questions_2 = eval(history[j][3].replace("$",","))
                if order_info != {}:
                    response, answer_id = detect_response(shop,questions_2,order_info,message,history)
                    history[j][2] = order_info["customer_id"]

                elif "not_customer" in questions and "status" in questions_2:
                    question_2 = ["status","not_customer"]
                    order_info_2 = {"status":"none","time":400,"order_time":400}
                    response, answer_id = detect_response(shop,questions_2,order_info_2,message,history)
                    
                else:
                    response = "/ALERT I could not find info on this customer"
                    answer_id = "s9"
                    
            elif order_info != {}:
                response, answer_id = detect_response(shop,questions,order_info,message,history)
            else:
                response = "/ALERT I could not find info on this customer"
                answer_id = "s9"
            
            answer_ids = list(eval(history[j][4].replace("$",",")))
            if answer_id not in answer_ids:
                answer_ids.append(answer_id)
            else:
                return "/ALERT This customer often asks me the same question", "/ALERT"
            history[j][4] = str(answer_ids)
            lst = []  #rebuild the file content
            for lst3 in history:
                lst.append(",".join(lst3))
            cont = "\n".join(lst)
            file = open("shops/{}/history.csv".format(shop),"w")
            file.write(cont)
            file.close()
            return response, answer_id
            

    
 
    if order_info != {}:
        response, answer_id = detect_response(shop,questions,order_info,message,history)
        info = order_info["customer_id"]

    else:
        order_info_2 = {"status":"none","time":400,"order_time":400}
        response, answer_id = detect_response(shop,questions_2,order_info_2,message,history)
        info = "no"
    # Build the file content
    answer_ids = []
    answer_ids.append(answer_id)
    if is_buffalo:
        shop = "BUFFALO"
    file = open("shops/{}/history.csv".format(shop),"a")
    content = "\n{},{},{},{},{}".format(message["email"],str(datetime.now())[:-7],info,str(questions).replace(",","$"),str(answer_ids).replace(",","$").replace("\r",""))
    if is_buffalo:
        content += ",{}".format(shop)
    file.write(content)
    file.close()
    return response, answer_id