J
J
Jalal Nasirov2021-06-19 07:30:20
Python
Jalal Nasirov, 2021-06-19 07:30:20

How to determine that the data entered by the user is a number?

a = int(input('Your age > '))
If the user enters characters that are not numbers, then an error will occur because the variable is int. It is necessary that when the user writes any data that is not a number, a certain code occurs, and not an error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alan Gibizov, 2021-06-19
@Best_Loops

In Python, input() returns a string. A string has many methods, some of them are checking the string for belonging to one or another type of character. One such method, .isdigit() returns a boolean whether the string contains only digits. You can use this method to validate entered characters.

S
Snowdevil, 2021-06-19
@Snowdevil

if a == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' or a == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question