0

Turn string to integer only if it's possible.

a = input("- ") if int(a) == a: a = int(a) else: print("your input isn't an integer") It doesn't work because str(a) == int(a) -----> False but... float(a) == int(a) -----> True How can I turn a str to a int, but without an error if I don't put an integer? Thanks (sorry if i have a bad english)

29th Mar 2021, 2:32 AM
Mnl
Mnl - avatar
2 Respuestas
+ 7
Use try , except block. try: number=int(input()) except: number=0
29th Mar 2021, 2:36 AM
TOLUENE
TOLUENE - avatar
0
I type the float to int example because I can turn a float to an integer only if it's ".0": x = float(input("- ")) if x == int(x): print(int(s)) else: print(x)
29th Mar 2021, 2:34 AM
Mnl
Mnl - avatar