'How am I supposed to create a function that distinguishes int objects or float objects?

import math
a = 1
b = 1
exponents = 0
total = 0
result = 0
retroceder = 0
formati = ["/"]
formatt = [","]

def enter_data():

    global exponents

    a = input("Inserte 1er número: ")
    for c in formatt:
        a = a.replace(c,".")
    a = float(a)
    b = input("Inserte 2do número: ")
    for c in formatt:
        b = b.replace(c,".")

        if c in formatt:
            exponents += 1
            
    if exponents > 0:
        b = float(b)
    else:
        b = int(b)

    ask = input("¿Quiere tetrar(1) o pentar(2)? ")

    if ask == str(1):

        tetrar(a,b)

    elif ask == str(2):

        pentar(a,b)

    else:

        print("Repítamelo, por favor.\n",100*"-")
        enter_data()

def tetrar(a,b):

    global total, result, retroceder
    
    if b-1 <= 0 and b-1 > -1:

        ecuacion = 1 + (2*math.log1p(a)/1+math.log1p(a))*b - (1-math.log1p(a)/1+math.log1p(a))*b**2
        print(math.log1p(a),ecuacion)
        total = "a**ecuacion"

    elif b <= -1:

        total = "math.log(tetrar(a,b+1))"

    elif b > 0 and type(b) == "class int":

        b = int(b)
        torre = "a**"
        total = (b*torre)+str(a)

    else:

        #"b" is class float, but I want it to convert to int or float depending of if it had or not commas/points
        #that indicate that number is, in fact, a float.
        print("Class b: ", type(b))
        total = "tetrar(a,b-1)"
        retroceder += 1
        
    if retroceder > 0:

        torre = "a**"
        total = (retroceder*torre)+str(total)
        
    try:
        expression = []
        expression.append(str(total))
        result = eval(total)
    except TypeError:
        print("Hubo un error, esta es la expresión del cálculo no realizado, con a=",a,": ",expression[0])
    except OverflowError or MemoryError:
        print(expression[0])
        print("El número es muy grande, intente con otro.\n", 100*"-")
        enter_data()

    print(result,"tiene",int(math.log10(result)+1),"cifras")

enter_data()

The program is made to do tetrations https://en.wikipedia.org/wiki/Tetration and for that it needs to differentiate between int numbers or float numbers for the variable "b". Instead it sets "b" to be always float regardless of its content, and raises TypeError in every operation it does. Is there any method to distinguish ints and floats?

Any help will be appreciated.



Solution 1:[1]

use the builtin function isinstance to accomplish this:

>>> a=2
>>> isinstance(a,int)
True
>>> isinstance(a,float)
False

it will return True if the value passed as the first argument will be an instance of the class passed as second argument.

the function calls the __instancecheck__ method, you can read more on the documentation that i linked above.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 XxJames07-