Search code examples
pythonvariablesmultiplication

How do you look and see if there is a number in a raw_input for an equation calculator


I am trying to make an equation calculator I am trying to have a checker for the equation to see if there is a number before an x or y so that it can multiply it. how would I check to see if there is any number in the raw_input?

def x_pluse_y(total):
    x = raw_input('what is your x? ' )
    y = raw_input('what is your y? ' )
    if x == 'x':
        x = 0
    if y == 'y':
        y = 0
    float(x) + float(y) == float(total)
    step1 = float(total) - float(y)
    x = step1
    step2 = int(total) - int(x)
    y = step2
    print 'Your x is ' + str(x)
    print 'Your y is ' + str(y) 

def x_minus_y(total):
    x = raw_input('what is your x? ' )
    y = raw_input('what is your y? ' )
    if x == 'x':
        x = float(0)
    if y == 'y':
        y = float(0)
    if x > 0:
        x == x 
    elif y > 0:
        y == y
    if float(x) > 0:
        y = float(total) - float(x)
    if float(y) > 0 :
        x = float(total) - float(y)
    print 'Your x is ' + str(x)
    print 'Your y is ' + str(y) 

def multi_x(equation):
    x = raw_input('what is your x? ' )
    y = raw_input('what is your y? ' )
    if x == 'x':
        x = float(0)
    if y == 'y':
        y = float(0)
    if x > 0:
        x == x 
    elif y > 0:
        y == y
    if float(x) > 0:
        y = float(equation) - float(x)
    if float(y) > 0 :
        x = float(equation) - float(y)
    print 'Your x is ' + str(x)
    print 'Your y is ' + str(y)

Solution

  • This will only work for an input of only numbers, not a mix of number or letters (but I can modify this to allow for that)

    def CheckStrIfVar(var):
        try:
            #If it is not a number this will raise an error, hence why I'm using exceptions
            test = int(var)
            return True
    
        except:
            #Will run if it cannot convert it to a var
            return False