Search code examples
pythonpython-3.xdatabasepython-telegram-bot

Unable to get data collector to display values (Keeps displaying None value)


Hello I am currently using python telegram bot to create my bot, currently I am unable to use my data collector (which also functions to calculate my calories) to display the value I want) The issue is that it keeps returning a None value, does anybody know the solution why ?

def cal_collector(user_input):
    cal = []
    if user_input.isnumeric() == True:
        cal.append(user_input)
    else:
        if str(user_input).lower == "math":
            if cal == False:
                return "0"
            total_calories = sum(cal)
            return str(total_calories)
   


def cal_calculator(update: Update,_: CallbackContext):
    query = update.callback_query
    query.answer()
    keyboard = [
        [InlineKeyboardButton("Back", callback_data = str(ONE))]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    
    query.edit_message_text(str(cal_collector("math")),reply_markup = reply_markup)
    return SECOND

def calorie_reply(update: Update,_ : CallbackContext):
    keyboard = [
        [InlineKeyboardButton("Back", callback_data = str(ONE))]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    user_input = update.message.text 
    cal_collector(user_input)
    update.message.reply_text(calorie_input(user_input),reply_markup = reply_markup)
    return SECOND

I understand my means of doing this is quite rubbish :') so if anybody has other better solutions to better store my data in python telegram bot it would be much appreciated! ( I am still not too sure how to utilise classes and objects for this tho)


Solution

  • Nevermind, turns out the issue was that the values were being thrown away as said by @BoarGules. Also, I restructured the code in such a way such that it was much simpler. Although it does not have the isnumeric filter yet for the user input but i will do what I can. Here is the appended code for anyone that is interested.

        answer = "You have keyed in " + user_input + " calories! Click Back to go back to the main menu"
        
        return answer
        
    #Storing Data in a class 
    
    cal = []
    
    # calculates total cals
    def cal_calculator(update: Update,_: CallbackContext):
        query = update.callback_query
        query.answer()
        keyboard = [
            [InlineKeyboardButton("Back", callback_data = str(ONE))]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)
        
        
        
        query.edit_message_text(f'You have consumed {str(sum(cal))}',reply_markup = reply_markup)
        return SECOND
    
    
    
    def calorie_reply(update: Update,_ : CallbackContext):
        keyboard = [
            [InlineKeyboardButton("Back", callback_data = str(ONE))]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)
        user_input = update.message.text 
        user_int = int(user_input)
        cal.append(user_int)
        update.message.reply_text(calorie_input(user_input),reply_markup = reply_markup)
        return SECOND