How to make python accept both int and float input??? | Sololearn: Learn to code for FREE!


How to make python accept both int and float input???

i need my calculator to accept both the inputs in form of integer and floating point number Neeeeed Hellp ASAP

4/30/2018 2:12:56 PM

ᏳoƊoԲᎮc !

113 Answers

New Answer


You could just use float, but if you absolutely must know whether the end user has input an int or a float you could check the input string to see if it contains a '.' i = input() if '.' in i: i = float(i) else: i = int(i)


you can just do float(input()) and if the sum is an even number you can make an if statement to print it as an int since you can pass int values to float it doesnt have to have the .12 at the end it will calculate it as float


You can use the eval() function just instead of int() and float()... Int function takes the input as integer value wheraes the float function takes it as a decimal value....but if u need to I put both tha values without adding a conditional statements in the code and without can use the eval() function which stands for it name suggest it evaluates the inputted also supports string values... For eg a=eval(input ("enter first no :")) b=eval(input("enter 2nd no :")) print ("Sum : ",a+b) Try Here :


Post the link to the code please.


The modulus method can also be used. If the number does not have any decimal part it will be considered as an integer. Num=float(input("Num: ")) if Num%1 ==0: Num=int(Num)



For integer input like n n=int(input()) For float input : float(input())


By using eval() function , we gate input as int() and also float() User = input(eval("Enter your no"))


What is eval() If we want we take input as particular data type we use eval function N=eval(input())


N=int(input ()) F=float (input ())


int() for integers float() for floating point Eval() for both int and float


We can use "if statement" I think


Frantz Etienne You are sadly mistaken. Python will not do an implicit conversion here. The result of x + y in your case would be 6.53 As both x and y are strings, due to that being the value type that is returned from the input() function, and string concatenation is what would take place in your posted code. Also, you should not use a built-in function or class name, etc, such as 'sum', for the name of one of your variables, as it will shadow the name for that function/class etc.


How will Improve my coding skills please


a = (lambda x: int(x) if x.isdecimal() else float(x))(input()) print("value {} and type {}".format(a,type(a)))


You can use: int(input ()) or eval(input ()) or float (input ())


It's very simple all you need to do is use float before asking to input the data i.e. int_float=float(input ("Enter the value:")) Suppose user inputs 45.9 or 3 both can be accepted using float


You can use eval() function


Simply use n=int(float(input()))


X = int(input()) Y = float(input())