How to check if input is float or int?

后端 未结 3 2067
灰色年华
灰色年华 2020-11-29 13:55

I want to make a simple converter, to print either hexadecimal number of float or integer. My code is:

number = input(\"Please input your number...... \\n\")         


        
相关标签:
3条回答
  • 2020-11-29 14:35

    I wanted to make a simple converter that would convert decimal or float number to Hexadecimal using Python3. But before that I wanted to add a simple check if the number entered through command prompt is decimal or hexadecimal. But after some searching I found that the problem with input() function of Python3 is that anything entered through terminal using this function will always print as string and doesn't evaluate it. So we can't apply check condition directly. Now there is one possibility to use data-conversion function like number = int(input("Enter the number")) or number = float(input("Enter the number")) but here the main problem is we can only check one condition at a time, either, float or integer. Like the one shown below:

    number = float(input("Enter the number"))
    if type(number) == float:
        print("Entered number is float and it's hexadecimal is:", float.hex(number))
    else:
        print("you entered an invalid number") 
    

    Or the same method can be used for int with little modification like:

    number = int(input("Enter the number"))
    if type(number) == int:
        print("Entered number is, ", number,"and it's hexadecimal is:", hex(number))
    
    else:
        print("you entered an invalid number")
    

    But here we can only enter integer because float can not be converted into integer using int() function. It will give the following error:

    invalid literal for int() with base 10: '2.2'

    0 讨论(0)
  • 2020-11-29 14:36

    TLDR: Convert your input using ast.literal_eval first.


    The return type of input in Python3 is always str. Checking it against type(2.2) (aka float) and type(2) (aka int) thus cannot succeed.

    >>> number = input()
    3
    >>> number, type(number)
    ('3', <class 'str'>)
    

    The simplest approach is to explicitly ask Python to convert your input. ast.literal_eval allows for save conversion to Python's basic data types. It automatically parses int and float literals to the correct type.

    >>> import ast
    >>> number = ast.literal_eval(input())
    3
    >>> number, type(number)
    (3, <class 'int'>)
    

    In your original code, apply ast.literal_eval to the user input. Then your type checks can succeed:

    import ast
    
    number = ast.literal_eval(input("Please input your number...... \n"))
    if type(number) is float:
        print("Entered number is float and it's hexadecimal number is:", float.hex(number)) 
    elif type(number) is int:
        print("Entered number is, ", number,"and it's hexadecimal number is:", hex(number)) 
    else:
        print("you entered an invalid type")
    

    Eagerly attempting to convert the input also means that your program might receive input that is not valid, as far as the converter is concerned. In this case, instead of getting some value of another type, an exception will be raised. Use try-except to respond to this case:

    import ast
    
    try:
        number = ast.literal_eval(input("Please input your number...... \n"))
    except Exception as err:
        print("Your input cannot be parsed:", err)
    else:
        if type(number) is float:
            print("Entered number is float and it's hexadecimal number is:", float.hex(number)) 
        elif type(number) is int:
            print("Entered number is, ", number, "and it's hexadecimal number is:", hex(number)) 
        else:
            print("you entered an invalid type")
    
    0 讨论(0)
  • 2020-11-29 14:47

    input returns a string, you are trying to check whether it looks like a float or an integer:

    number = input("Enter your number? ")
    try:
        number = int(number)
        print("Entered number is, ", number,"and it's hexadecimal number is:", hex(number))
    except ValueError:
        try:
            number = float(number)
            print("Entered number is float and it's hexadecimal number is:", float.hex(number)) 
        except ValueError:
            print("You entered an invalid number")
    
    0 讨论(0)
提交回复
热议问题