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

+22

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 !

31 Answers

New Answer

+83

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)

+12

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

+5

Post the link to the code please.

+3

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

+3

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

+1

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

+1

Without specifying datatype we can insert any kind of values For e.g., a=input("enter value") the value to be insert can be of any datatype

+1

You can use eval(), it simple take input that we gave. a=eval(input()) print(a) '''we can give it any data type but we have to write that data type in its format, foe example giving int 21 or float 23.2, or string 'Yo'(text should be closed in quotes) '''

+1

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

0

0

Use eval() function.

0

How to improve coding

0

Give 2 variables with the inputs , with one having int() that is integer and the other float() that is float Smth like dis A = input(int()) B = input(float())

0

Omg 👁️👄👁️

0

Use eval () function

0

Hi bro I want to say u ki who is

0

0

0

Use isinstance fonction a=input() If isinstance (a, float) # or (a,int) ... Or type (a)

0

By using "If" and "Else" condition you can firs check the input type and then impliment in your code.....