r/PythonLearning • u/FormalRecord2216 • Jun 11 '25
is int funtion nor working in vs?
So i wrote this code If i put an integer as an input it works But if i put in any non integer it doesnt seem to work at all Shows this error Can anyone help me understand my mistake
1
u/Fit_Sheriff Jun 11 '25
Use float instead of int as you are using decimal Point number in the int which isn't valid though you could use float(y) Instead and get the same result
1
1
u/OkWitness7392 Jun 11 '25
A single character that is a letter cannot be converted to an integer; only strings consisting of numeric digits can be converted.
x = 'a'
y = int(x) # Error
1
1
u/RailRuler Jun 11 '25
Input () returns a string.
Int() takes something that is valid for converting to an integer.
Strings with decimal points are not valid for converting to an integer, only to a float.
1
u/FoolsSeldom Jun 11 '25
intapplied to astrwill expect and convert a whole number decimal digit sequence of characters - anything else will failintapplied to afloatwill trim the number of the decimal portion after the.thus,
intapplied to astrcontaining a decimal floating point number representation will failintcan also be used for number base convertion, e.g.int("101", 2)will treat the string as a binary sequence and convert to decimal
1
u/HuygensFresnel Jun 11 '25
Int(3.5) returns 3. Int(“3.5”) crashes because it would first have to go thorough float interpretation and it doesnt make that assumption. You should then do int(float(“2.5”))
0


9
u/Fit_Sheriff Jun 11 '25
Use float instead of int as you are using decimal Point number in the int which isn't valid though you could use float(y) Instead and get the same result