+ 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
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)
+ 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
+ 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
+ 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)
+ 9
Post the link to the code please.
+ 7
By using eval() function , we gate input as int() and also float()
User = input(eval("Enter your no"))
+ 7
int() for integers
float() for floating point
Eval() for both int and float
+ 5
What is eval()
If we want we take input as particular data type we use eval function
N=eval(input())
+ 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
+ 4
How will Improve my coding skills please
+ 4
For integer input like n
n=int(input())
For float input : float(input())
+ 4
N=int(input ())
F=float (input ())
+ 4
We can use "if statement" I think
+ 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
+ 3
a = (lambda x: int(x) if x.isdecimal() else float(x))(input())
print("value {} and type {}".format(a,type(a)))
+ 3
You can use: int(input ()) or eval(input ()) or float (input ())
+ 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
+ 3
X = int(input())
Y = float(input())
+ 3
If you want to accept input in form of integer:
a=int(input("enter a number"))
+ 2
Simply use n=int(float(input()))