r/PythonLearning • • 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

6 Upvotes

41 comments sorted by

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

1

u/IvariialzZ Jun 11 '25

this is not right, decimal point numbers are allowed for int() the problem is not that he is adding decimal point numbers the problem is that he is using int to convert a string with a decimal point. The "." is not read as a base 10 and therefore It gives an error.

int(5.64) = 5 int("5.54") = error its expected and happens the same with round round(5.64)=6 round("5.64")=error

-2

u/FormalRecord2216 Jun 11 '25

Thank you But then what is the int function for? Isnt it used for removing the decimals for integers Sorry if i am being a nuisace But i am new and i think i misunderstood something

4

u/Fit_Sheriff Jun 11 '25

Nope the int will throw error and if you want to round off use "round" function

1

u/FormalRecord2216 Jun 11 '25

Ok thanks for that But then what does the int function do? I thought it used to round stuff up like

Int(4.36) =4

In my code when lets say 5.26 is equal to y Shoudnt x just become 5 ?

5

u/lolcrunchy Jun 11 '25

Using int on a float takes the integer part of the number:

int(5.36) == 5

Using int on a string attempts to convert the entire string into an integer:

int('4') == 4

These are essentially two different operations.

This breaks because the argument is a string, which means int() is trying to create an integer from text:

int('5.36')

1

u/Fit_Sheriff Jun 11 '25

It helps you to convert string to int. That's it my friend

2

u/Fit_Sheriff Jun 11 '25

And if you wanna do it both then you first need to covert it float then it will be able to remove the decimal part

1

u/SCD_minecraft Jun 11 '25

int() isn't a function, it's a class

A type

2

u/Beautiful_Watch_7215 Jun 11 '25

Seems a bit like a function here. https://www.w3schools.com/python/ref_func_int.asp

2

u/SCD_minecraft Jun 11 '25
print(type(print)) # <class 'builtin_function_or_method'>

print(type(int)) # <class 'type'>

-1

u/Beautiful_Watch_7215 Jun 11 '25

Cool story, bro. Int is a class. Int() is a function. You can type more, copy / paste more, continue to believe anything you want.

2

u/PureWasian Jun 11 '25 edited Jun 11 '25

No need to be an ass to get your point across. int() is a constructor call, plain and simple. Neither a function nor a class.

EDIT: I will defer to this comment for the most proper explanation: https://www.reddit.com/r/PythonLearning/s/vrUtXaAxie

→ More replies (1)

1

u/SCD_minecraft Jun 11 '25

int() is litteraly same thing as int, just called

You cad add () to any object. It doesn't change it's type

0

u/Beautiful_Watch_7215 Jun 11 '25

Int() returns something. Specifically a member of the class Int. A class does not return something. But you can continue to believe parentheses have no meaning. I gave you a reference.

→ More replies (14)

1

u/PureWasian Jun 11 '25 edited Jun 11 '25

Sharing this comment for more visibility, given the misinterpretation in convo leading up to it: https://www.reddit.com/r/PythonLearning/s/vrUtXaAxie

TL;DR - int() is a constructor call, plain and simple. Neither a function nor a class.

1

u/Temporary_Pie2733 Jun 11 '25

Sidestepping the flame war, even the Python documentation lumps a lot of built-in types into its list of built-in functions. Accept that the type int is a function, as type is one of many built-in callable types.

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

u/Blakex123 Jun 11 '25

Integers are whole numbers. You will want to use float instead.

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

u/wirrexx Jun 11 '25

float(input(“Number: “))

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
  • int applied to a str will expect and convert a whole number decimal digit sequence of characters - anything else will fail

  • int applied to a float will trim the number of the decimal portion after the .

  • thus, int applied to a str containing a decimal floating point number representation will fail

  • int can 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

u/Kannan_27 Jun 11 '25

seems the file is not saved buddy