Jul-09-2020, 04:54 AM
(This post was last modified: Jul-09-2020, 04:54 AM by majorjohnusa.)
Dear all,
I have a rookie question.
In the following codes, we know that the second one will result in a ValueError, because the "int()" command cannot pass a string representation of a float into int. But why "float()" can do the opposite? Why "float()" can pass a string representation of an integer into float, when int() cannot pass a string of float into integer?
Many thanks in advance!
I have a rookie question.
In the following codes, we know that the second one will result in a ValueError, because the "int()" command cannot pass a string representation of a float into int. But why "float()" can do the opposite? Why "float()" can pass a string representation of an integer into float, when int() cannot pass a string of float into integer?
print (int("4")) print (int("4.1")) print (float("4")) print (float("4.1"))I know how to solve it by converting the string representation of a float into float first: print (int(float("4.1"))), but I am just curious why.
Many thanks in advance!