0

Convert input to int in python

int(input()) ? Gives me an error?

22nd Feb 2021, 9:58 AM
Benjamin Ian Awumsuri Benjamin Moshi
Benjamin Ian Awumsuri Benjamin Moshi - avatar
6 Answers
+ 2
If you want an input string to be an int even when the input is a float just wrap the float() with an int(). The int() can convert a float to int, but not a string with float text "3.2". int(3.2) # ok int("3.2") # error int(float("3.2")) # ok
22nd Feb 2021, 10:22 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
What error ? How you tried..Can you Share the saved code..
22nd Feb 2021, 10:06 AM
Jayakrishna 🇼🇳
+ 1
i found the problem, thanks for the help though,... its just this on practice 26.2 thats causing an error
22nd Feb 2021, 10:25 AM
Benjamin Ian Awumsuri Benjamin Moshi
Benjamin Ian Awumsuri Benjamin Moshi - avatar
0
maybe you give float or invalid int... you could try float(input()) ;)
22nd Feb 2021, 10:11 AM
visph
visph - avatar
0
float(input()) works perfectly
22nd Feb 2021, 10:12 AM
Benjamin Ian Awumsuri Benjamin Moshi
Benjamin Ian Awumsuri Benjamin Moshi - avatar
0
int: parse to integer float: parse to decimal
22nd Feb 2021, 10:13 AM
visph
visph - avatar