+ 2
Как проверить строку?
Как мне проверить строку на то, могу ли я её перевести в int? Чтобы моя программа говорила при следующих значениях подобное: "42" = можно "3А" = нельзя Спасибо. [Google translate] How can I check a string to see if I can translate it to int? So that my program says the following with the following values: "42" = possible "3A" = not allowed
5 Answers
+ 2
a = '42'
try:
int(a)
print('possible')
except ValueError:
print('not allowed')
if you try anything but a number it will not allow it.
NOTE: if you try with float numbers as strings, like '5.7' it will not allow it
+ 3
Amiran Bogomol
May be this code help you to know try- except
https://code.sololearn.com/cH8qzZ4NAOEE/?ref=app
+ 1
Abhay вводится будут разные числа
Different numbers will be entered.
0
a="42"
if all(i.isdigit() for i in a):
print("possible")
else:
print("not allowed")
0
By different numbers you mean?
You can enter any string and it will check if it contains all numbers or not ,if all are numbers then it will print possible otherwise not allowed and then you can perform other operations accordingly
Let me know if the solution i provided didn't helped for what you are actually looking for ,