1

im making a 'guess the number' telegram bot, that is supposed to come up with an integer from 0 to 100. When the user tries to guess the number, bot should give them hints, showing whether their guess is smaller or bigger than the secret number. But it seems to change the secret number every time it recieves a new guess from the user.

Here's the whole code

import random
import telebot
from telebot.types import Message
bot = telebot.TeleBot("-token-")


@bot.message_handler(commands = ('start'))
def guess_number():
   a = random.randint(0,100)
   a = guess_number.variable
def send_welcome(message):
   bot.send_message(message.chat.id, "you entered a guessing game, traveller. Guess a number from 0 to 100, or be eaten by a  dragon.")

@bot.message_handler(func=lambda message: True)
def checking(message):
    try: 
        user_message = int(message.text)
        if user_message > guess_number.variable:
            bot.send_message(message.chat.id, "your number is greater than mine")
        elif user_message < guess_number.variable:
            bot.send_message(message.chat.id, "your number is smaller than mine")
        else:
            bot.send_message(message.chat.id, "yep, that's the right number")
    except ValueError:
            bot.send_message(message.chat.id, "you must enter an integer")
bot.polling()

a = random.randint(0,100) a = guess_number.variable also it says that the variable 'a', which is a store for the secret number, is out of reach in guess_number function. I believe that the variable should be put somewhere else, but can't figure out where exactly.

Mistann
  • 25
  • 4
  • I'm not quite sure your `guess_number` function is reasonable. What do you expect `guess_number.variable` to be? And why do you 'overwrite'/reassign `a` to be that? Also, in `checking` the only line that needs to be in the `try` block is the first one. Everything else can be put into an `else` block using `try/except/else` structure – MatBBastos Nov 18 '21 at 16:24
  • 1
    Anyway: you need to store the random number somewhere. That cannot be in a local variable, cause as soon as you change scopes, you'll lose the value, and calling the function again would (as expected) generate a new random number. You can store it in a global variable, or restructure the code to use a class instead of individual functions, which would allow to create a class attribute to store and access relevant values in different methods – MatBBastos Nov 18 '21 at 16:28

1 Answers1

1

For mutliple chats

If you want it to work for multiple users, you'll need a database of some sorts to store a number for each chat. A dictionary could work fine as a small example.

numbers = dict()

@bot.message_handler(commands=('start'))
def guess_number(message):
    numbers[message.chat.id] = random.randint(0, 100)

@bot.message_handler(func=lambda message: True)
def checking(message):
    try:
        user_message = int(message.text)
        if user_message > numbers[message.chat.id]:
            bot.send_message(message.chat.id, "your number is greater than mine")
        elif user_message < numbers[message.chat.id]:
            bot.send_message(message.chat.id, "your number is smaller than mine")
        else:
            bot.send_message(message.chat.id, "yep, that's the right number")
    except ValueError:
        bot.send_message(message.chat.id, "you must enter an integer")
    except (TypeError, KeyError):
        bot.send_message(message.chat.id, "there's no number to guess! say /start to start")

For a single chat

If you only need this bot to work for a single chat, you could opt to use a single global variable to store the secret number. Then make it accessible within functions with the global keyword.

the_number = None

@bot.message_handler(commands=('start'))
def guess_number(message):
    global the_number
    the_number = random.randint(0, 100)

@bot.message_handler(func=lambda message: True)
def checking(message):
    global the_number
    try:
        user_message = int(message.text)
        if user_message > the_number:
            bot.send_message(message.chat.id,"your number is greater than mine")
        elif user_message < the_number:
            bot.send_message(message.chat.id,"your number is smaller than mine")
        else:
            bot.send_message(message.chat.id, "yep, that's the right number")
    except ValueError:
        bot.send_message(message.chat.id, "you must enter an integer")

In either case, the numbers will be lost when the bot is off. Choose what fits and keep this in mind

Queuebee
  • 651
  • 1
  • 6
  • 24
  • Thanks so much, i figured out that `send_welcome()` and `guess_number()` should be fused in one function, so that both of them would be accesible shen the /start command is given. I also wrote an additional if, that checks if the secret number still equals None (that can happen if the user forgot to enter /start) owerall, everything is working just fine now:) – Mistann Nov 19 '21 at 08:51