Why my telegram bot (aiogram) doesn't work right?

Viewed 26

the bot offers to guess a number from 0 to 10. But for the correct answer, it answers "False"

I guess the problem is using asynchronous functions, but I'm not good at them

from aiogram import Bot, Dispatcher, executor, types
import random

bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot)

# number to guess
NUMBER = 0


# comes up with a number
def setRand():
    NUMBER = random.randrange(10)
    print(NUMBER)


# handler for /start
@dp.message_handler(commands=['start'])
async def start(msg: types.Message):
    setRand()
    await msg.answer("Try to guess the number from 0 to 10")


# handler for getting answer
@dp.message_handler()
async def getNumber(msg: types.Message):
    if msg.text == str(NUMBER):
        await msg.answer('True!')
    else:
        await msg.answer('False :с')
        await msg.answer('But I came up with new number!')

        setRand()


executor.start_polling(dp)```
1 Answers

Use global for global variables!

def setRand():
    global NUMBER
    NUMBER = random.randrange(10)
    print(NUMBER)```
Related