Как проверить строку? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

17th May 2020, 8:58 PM
Amiran Bogomol
Amiran Bogomol - avatar
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
17th May 2020, 9:21 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 3
Amiran Bogomol May be this code help you to know try- except https://code.sololearn.com/cH8qzZ4NAOEE/?ref=app
17th May 2020, 9:24 PM
Petr
+ 1
Abhay вводится будут разные числа Different numbers will be entered.
17th May 2020, 9:09 PM
Amiran Bogomol
Amiran Bogomol - avatar
0
a="42" if all(i.isdigit() for i in a): print("possible") else: print("not allowed")
17th May 2020, 9:07 PM
Abhay
Abhay - avatar
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 ,
17th May 2020, 9:09 PM
Abhay
Abhay - avatar