D
D
danik2022-01-05 15:29:43
Python
danik, 2022-01-05 15:29:43

Why is there a key error when creating a dictionary in telebot?

class Player():
    def __init__(self, name) -> object:
        self.ref = 0
        self.bal = 0


user_ur = {}


@bot.message_handler(content_types=['text'])
def send_text(message):
    global user_ur
    msg = message.chat.id
    if message.text == 'Balance':
        print(str(user_ur[msg].ref))
        bot.send_message(message.chat.id,
                         ' Your personal balance: 0.00' + ' Emerald' + '\n' + '\n' + ' Total partners: ' +
                         str(user_ur[msg].ref))


I make a bot through telebot. I want to make a variable for each user through a dictionary where the unique value is message.chat.id. But when entering a message in the bot, an error occurs print(str(user_ur[msg].ref))
KeyError: 1200514316

Why does the error occur and how to fix it? who will answer the question I will give a pack of chips

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question