+ 66

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

30th Apr 2018, 2:12 PM
ᏳoƊoÔČᎼc !
ᏳoƊoÔČᎼc ! - avatar
154 Answers
+ 416
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)
30th Apr 2018, 2:43 PM
ChaoticDawg
ChaoticDawg - avatar
+ 36
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
30th Apr 2018, 3:53 PM
Markus Kaleton
Markus Kaleton - avatar
+ 28
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
8th Jun 2021, 3:51 AM
NANDAN
NANDAN - avatar
+ 11
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)
30th Sep 2021, 4:32 PM
Dharshini .P.G
+ 9
Post the link to the code please.
30th Apr 2018, 2:16 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 7
By using eval() function , we gate input as int() and also float() User = input(eval("Enter your no"))
24th Sep 2021, 12:36 PM
Hambire Ranjeet
Hambire Ranjeet - avatar
+ 7
int() for integers float() for floating point Eval() for both int and float
27th Jan 2022, 8:40 PM
Md Furkan
Md Furkan - avatar
+ 5
What is eval() If we want we take input as particular data type we use eval function N=eval(input())
18th Oct 2021, 1:59 AM
Bobby
Bobby - avatar
+ 5
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
3rd Feb 2022, 2:24 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
How will Improve my coding skills please
16th Mar 2021, 9:38 PM
Litima Nuhu
+ 4
For integer input like n n=int(input()) For float input : float(input())
15th Apr 2021, 4:46 PM
KOPPULA DAMODAR REDDY
KOPPULA DAMODAR REDDY - avatar
+ 4
N=int(input ()) F=float (input ())
30th Dec 2021, 10:05 PM
Emmanuel Obu
Emmanuel Obu - avatar
+ 4
We can use "if statement" I think
2nd Feb 2022, 5:01 AM
yonas
yonas - avatar
+ 3
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
29th Mar 2021, 11:55 AM
Diya Namdev
Diya Namdev - avatar
+ 3
a = (lambda x: int(x) if x.isdecimal() else float(x))(input()) print("value {} and type {}".format(a,type(a)))
5th Jun 2021, 2:06 PM
Naveen Surya
Naveen Surya - avatar
+ 3
You can use: int(input ()) or eval(input ()) or float (input ())
13th Oct 2021, 7:54 AM
Gabriel
Gabriel - avatar
+ 3
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
22nd Jan 2022, 6:56 AM
Jaya Vishwakarma
Jaya Vishwakarma - avatar
+ 3
X = int(input()) Y = float(input())
18th Jun 2022, 8:12 AM
EmmzyCodez
EmmzyCodez - avatar
+ 3
If you want to accept input in form of integer: a=int(input("enter a number"))
16th Jul 2022, 3:19 PM
Mahima Mohan
+ 2
Simply use n=int(float(input()))
6th Jun 2022, 9:13 AM
Suraj M