Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question