N
N
Nikolai Ostrovsky2020-10-03 19:49:58
Python
Nikolai Ostrovsky, 2020-10-03 19:49:58

How to determine the right numbers without error?

The code
elif int(message.text) <= 14:

Everything is fine when a number is entered, but when sending any other text (except other keywords) it crashes with an error.

Mistake
elif  int(message.text) <= 14:
ValueError: invalid literal for int() with base 10: 'выфв'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-10-03
@KotoFeyic

Use string method isdigit()
https://pythonz.net/references/named/str.isdigit/

if message.text.isdigit():
    if int(message.text) <= 14:

M
milssky, 2020-10-03
@milssky

Or use try/catch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question