#Basic Python doubt

22 messages · Page 1 of 1 (latest)

west gull
#

x=input('value')
y=int(x)
print (y+40)
Why does it not work for decimal values , but works finely for integrals
Started python today

hasty scarab
#

int() converts to integers

#

Integers can't have decimal values (aka floating point) -> it breaks

#

Try using float() instead

west gull
#

x=int(2.3)
Print (x) gives 2

#

How is it working for decimals here

west gull
#

But it gives an error

#

Works fine for integral inputs

west gull
#

As input

hasty scarab
hasty scarab
west gull
#

When we use int(2.3)

hasty scarab
#
x = input() # type(x) = str
y = int(x) # exception, can't convert it properly

x = 2.5 # type(x) = float
y = int(x) # y = 2
west gull
#

Yeah got the answer

#

Int function can only convert integrals (in form of strings) to numbers

#

Like x="4" ( any integral value)
y= int (x)
print(y) gives 4

#

But x= "2.3"
y=int(x)
Shows the same error