Answer the question
In order to leave comments, you need to log in
Restricting input to a variable?
Hello, I want to make a Command /random
So that when a number is entered greater than 1000 or letters, the bot writes so that he writes a number less than 1000 or he enters letters
@dp.message_handler(commands=['random'])
async def process_start_command(message: types.Message):
number1 = int(message.text.split()[1])
random_number = random.randint(0, number1)
if not 1 <= number1 <= 1000:
await message.answer(f'введите число меньше 1000')
Answer the question
In order to leave comments, you need to log in
You need to check before converting to a number, plus add a check for the number itself (or the absence of letters, if desired)
async def process_start_command(message: types.Message):
number1 = message.text.split()[1]
if not (number1.isdigit() and 1 <= int(number1) <= 1000):
await message.answer(f'введите число меньше 1000')
else:
random_number = random.randint(0, int(number1))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question