D
D
didrux2020-07-29 22:53:55
Python
didrux, 2020-07-29 22:53:55

Validation of int, str, float?

Good afternoon. I'm learning Python.

There is a code:

a = input()
if a == '123'
print('Hello world')


Question: how to make validation of the entered message by str, int, float types?
For example: float type validation: I enter 123, I get hello world, I enter 5.23 I get hello world, I enter wtfsomething I get print('you entered not a number, enter a number') and for input() to happen again, I enter hg#*$#4 I get print('you didn't enter a number, enter a number') and so that input() happens again and so on. How to do it??
I see it like this:
if a = float(a)

But it doesn't work.
Thanks a lot!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Timur Pokrovsky, 2020-07-29
@didrux

try/except
https://pythonworld.ru/tipy-dannyx-v-python/isklyu...

P
PavelMos, 2020-07-30
@PavelMos

There are functions isint, isnumeric, isalpha and so on. In Russian here
https://pythonworld.ru/tipy-dannyx-v-python/stroki...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question