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

+28

# 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

+151

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)

+16

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

+10

+9

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

+6

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

+5

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

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

+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)))

+1

You can just use the if statement Num = input () If a in Num: I = float(a) else: I = int(a)

0

How will Improve my coding skills please

0

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

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.....

0

If you want to make it aspect both float and integer then use float function because we can't do float operation in integer but in float we can do both float and integer operation so use float function

0