B
B
beckonmeon2022-04-06 20:54:25
Python
beckonmeon, 2022-04-06 20:54:25

What to do with a mistake in writing a bot for the telegram game Guess the number?

There is a bot code that thinks of a number from 1 to 100, I want to make a code without using a global variable, I rummaged through a lot of materials, but it gives an error that I can’t overcome using this very global random variable.

Code

import random
import telebot

bot = telebot.TeleBot('токен')

@bot.message_handler(commands=['help', 'start'])
def welcome(message):
    bot.reply_to(message, 'Поиграем в "Угадай число"?\nЯ загадал число от 1 до 100, сможешь угадать?')
    n = random.randint(1, 100)
    bot.register_next_step_handler(message, number_guess, n)

def number_guess(message, n):
    try:
        number = message.text
        if not number.isdigit():
            bot.send_message(message.chat.id, 'Это не число')
            bot.register_next_step_handler(message, number_guess)
            return
        if int(number) > n:
            bot.send_message(message.chat.id, ("Ваше число больше загаданного"))
            bot.register_next_step_handler(message, number_guess)
        elif int(number) < n:
            bot.send_message(message.chat.id, ("Ваше число меньше загаданного"))
            bot.register_next_step_handler(message, number_guess)
        else:
            bot.send_message(message.chat.id, ("Угадал"))
    except ValueError:
        bot.send_message(message, ("это не число"))



bot.polling(none_stop=True, interval=0)

The error code itself

Path\venv\Scripts\python.exe Path/main.py
Traceback (most recent call last):
File "Path\main.py", line 35, in
bot.polling(none_stop=True, interval=0)
File "Path\venv\lib\site-packages\telebot\__init__.py", line 658, in polling
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
File "Path\venv\lib\site-packages\ telebot\__init__.py", line 720, in __threaded_polling
raise e
File "Path\venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
self.worker_pool.raise_exceptions()
File "Path\venv \lib\site-packages\telebot\util.py", line 135, in raise_exceptions
raise self.exception_info
File "Path\venv\lib\site-packages\telebot\util.py", line 87, in run
task(*args, **kwargs)
TypeError: number_guess() missing 1 required positional argument: 'n '

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2022-04-06
@beckonmeon

@bot.message_handler(func=lambda message: True)
Remove decorator. Why is it, if the function works only with the help ofregister_next_step_handler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question