IF condition not valid for data type verification
-
At the initial stage of Python, I can't figure out why it doesn't work.
print('Введите цифры: ') a = input() if (type(a) == int): print('Вы ввели цифры: ', a) else: print('Вы ввели не цифры, а другие символы!')
I also tried the second version of the code. It doesn't work anyway. ♪ ♪
print('Введите цифры: ') a = input() if isinstance(a, int): print('Вы ввели цифры: ', a) else: print('Вы ввели не цифры, а другие символы!')
If the second line writes a = int(input()), it makes a mistake File "C:/PYTHON/Studies/lesson2.py, line 2, in a = int(input() ValueError: invalid literal for int() with base 10: 'gj'
-
a = input() if (type(a) == int):
input
Always returning the line, so it can't be a number.You have to make that code for the above purpose.
try:
value = int(input()) print(f'Вы ввели цифры: {value}')
except ValueError:
print('Вы ввели не цифры, а другие символы!')
The same option could be used:
value = input()
if value.isnumeric():
print(f'Вы ввели цифры: {value}')
else:
print('Вы ввели не цифры, а другие символы!')