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

+57

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

+322

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)

+30

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

+18

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 eror....you can use the eval() function which stands for evaluate...as it name suggest it evaluates the inputted value...it also supports string values... For eg a=eval(input ("enter first no :")) b=eval(input("enter 2nd no :")) print ("Sum : ",a+b) Try Here : https://code.sololearn.com/cQlDK925j2b1/?ref=app

+11

Post the link to the code please.

+8

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)

+7

https://code.sololearn.com/c1xFnov4LlEP/?ref=app

+5

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

+5

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

+4

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

+4

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

+4

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

+4

We can use "if statement" I think

+4

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. https://code.sololearn.com/cSPtAIi8QEyM/?ref=app

+2

How will Improve my coding skills please

+2

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

+2

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

+2

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

+2

You can use eval() function

+2

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

+2

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