T
T
Tomorrow7722021-06-17 00:28:32
Python
Tomorrow772, 2021-06-17 00:28:32

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')


How to do it?
I use the aiogram library

I will be glad if you help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-06-17
@Tomorrow772

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))

And by the way, random.randint(0, x) will return a number from the x+1 set of numbers, not x.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question