0

Today I'm trying to make a python program by taking a float and an integer as one input at same time. But it shows some error.

This is what I have tried:

a=float,int(input("ENTER YOUR PERCENTAGE: "))
Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32
Mayank
  • 25
  • 3

3 Answers3

0

Try this:

a = input()
a = float(a) if '.' in a else int(a)
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • This solution breaks if you input two numbers separated by an space, like '2 3'. Maybe this can be included in the conditional. – panadestein Jun 09 '20 at 11:54
  • if your input contain space separated numbers you can split the string then convert it – deadshot Jun 09 '20 at 11:58
  • Yes, but in the way it is implemented now this is missing, and you get an exeption if `input` gets two space separated numbers, with I think is what OP asked. Just a suggestion. – panadestein Jun 09 '20 at 12:00
  • @panadestein op mentioned in comment input will contain single number then only i written the solution – deadshot Jun 09 '20 at 12:03
0

From what i understand you want to get a float and integer both at the same time. Try this code.Remember a is stored as a tuple.Use the index to get the values stored in it.

a=float(input("ENTER YOUR PERCENTAGE AS FLOAT: ")),int(input("ENTER YOUR PERCENTAGE AS INT: "))

If you want to store them seperately all you have to do is this small change.

my_float, my_int=float(input("ENTER YOUR PERCENTAGE AS FLOAT: ")),int(input("ENTER YOUR PERCENTAGE AS INT: "))
AfiJaabb
  • 316
  • 3
  • 11
  • Please make sure that you present your question correctly.You have said that you want to take a pair."Today i am try to make a python programm by taking a ***pair*** of float and integer as a input on same time. But it show some error . So please help me to ressolve this issue." – AfiJaabb Jun 09 '20 at 11:20
0

I think this solution (though somehow cumbersome) does exactly what you want, provided you input two numbers:

a = [func(float(val)) for func, val in zip([float, int],
                                input("ENTER YOUR PERCENTAGE: ").split())]

Note: the above code needs the additional float(val) inside func to avoid an exception that raises if the string representing the second number (that will be cast to int) contains dot characters.

panadestein
  • 1,241
  • 10
  • 21
  • if the user enter integer then your code converting it into float for 456 your code returning `[456.0]` – deadshot Jun 09 '20 at 12:09
  • Quoting the original post literally, it needs to read "a pair of float and integer as a input on same time", and my code works exactly for this I think. It is only meant to solve a particular problem :) – panadestein Jun 09 '20 at 12:13