Python Forum
TypeError: 'float' object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'float' object is not callable
#1
The first time everything works, but when I try to run the code on the second round, it gives an error after entering the variable "average". What could be the problem? Thanks)

import math
import telebot
from telebot import types
 
token='***'
bot=telebot.TeleBot(token)
 
texts = list()
 
# после запуска бота получаем меню из двух кнопок
@bot.message_handler(commands=['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1=types.KeyboardButton('Кнопка1')
    item2=types.KeyboardButton('Кнопка2')
    markup.add(item1, item2)
    bot.send_message(message.chat.id,'<b>Выберите одну из кнопок.</b>', parse_mode='html', reply_markup=markup)
 
# первый тип расчётов
@bot.message_handler(func = lambda message: message.text == 'Кнопка1')
def first(message):
    msg = bot.send_message(message.chat.id, '<b>Введите первое число.</b>', parse_mode='html')
    bot.register_next_step_handler(msg, second)
 
def second(message):
    global n
    n = int(message.text)
    msg = bot.send_message(message.chat.id, '<b>Введите второе число.</b>', parse_mode='html')
    bot.register_next_step_handler(msg, average)
 
def average(message):
    global average
    average = float(message.text)
 
    if average >= 5:
        bot.send_message(message.chat.id, f'<b>Введите коректные данные.</b>', parse_mode='html')
    x = 4.0
    while x < 5.01:
        if average >= x:
            pass
        else:
            averageNew = x - 0.049
 
            # минимальное значение
            average1 = average + 0.049
            n5 = (averageNew - average1) * n / (5 - averageNew)
            if math.ceil(n5) < 1:
                n5 = 1
            else:
                n5 = math.ceil(n5)
 
            # максимальное значение
            average2 = average - 0.049
            nN = (averageNew - average2) * n / (5 - averageNew)
            if math.ceil(nN) < 1:
                pass
            else:
                nN = math.ceil(nN)
 
            # запись расчётов в список
            if nN == n5:
                texts.append(f"{round(x, 2)} — {nN} шт.")
            else:
                texts.append(f"{round(x, 2)} — {n5}-{nN} шт.")
 
            x += 0.1
 
    bot.send_message(message.chat.id, f'<b>Список расчётов:</b>', parse_mode='html')
    bot.send_message(message.chat.id, "\n".join(texts), parse_mode='html')
 
# второй тип расчётов
@bot.message_handler(func = lambda message: message.text == 'Кнопка2')
def sorry(message):
    bot.send_message(message.chat.id, '<b>Данный тип калькулятора в разработке. Выберите другой вариант.</b>', parse_mode='html')
 
bot.polling(none_stop=True)
Reply
#2
Which average?
def average(message):
    global average
    average = float(message.text)
TimofeyKolpakov likes this post
Reply
#3
This cannot be:
def average(message):
    global average
When your python compiles your program it creates a variable named "average" that references a function. Later in the program you reassign that variable to be something else. Now "average" no longer references the function and you cannot call average(arguments).

This is a simpler example.
def average(*args):
    global average
    average = sum(args) / len(args)

print(average)
average(1, 2, 3, 4)
print(average)
average(1, 2, 3, 4)
Error:
<function average at 0x000001DD4E586160> 2.5 Traceback (most recent call last): File "c...", line 8, in <module> average(1, 2, 3, 4) TypeError: 'float' object is not callable
You can see in the output that "average" starts out referencing a function. The function is called and it reassigns average to be the average of 1, 2, 3, 4. Now there is no way to call the average function. The name (essentially what variables are in python) us being used to reference a float number, not a function.

To fix the problem you need to rename the function or the variable used to hold the float value.
TimofeyKolpakov likes this post
Reply
#4
Thank you all! Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 309 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 410 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 524 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 761 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,020 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,388 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 859 Jul-23-2023, 02:25 PM
Last Post: Fare
  TypeError: 'float' object is not callable #1 isdito2001 1 1,089 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,186 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  'SSHClient' object is not callable 3lnyn0 1 1,177 Dec-15-2022, 03:40 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020