Home > Software engineering >  Why does my python 2.7 code print 'false' when the components are true?
Why does my python 2.7 code print 'false' when the components are true?

Time:10-19

I'm working on two user defined functions, one that calls upon the first, that determines if inputs given are parallelograms or rectangles, but when I set my if statements, even if the inputs fulfill the "True" category for the latter function, it still prints as "False" (even when I replace the "False" print with something below, like print "No") I figured it was something the way I was calling upon the prior function in the latter statement.

#the function, isPara below works perfect
def isPara(s1, s2):
    '''if base lengths are same, it will return true'''
    if b1 == b2:
        isPara = True
        print 'True'
    else:
        isPara = False
        print 'False'

#however when I call isPara into isRec, the output displays as false even if it's true or doesn't #print false

def isRec(s1, s2, angle):
    '''if isPara is true '''
    if isPara is True:
        if angle == 90:
            isRec = True
            print 'True'
    else:
        isRec = False
        print 'Not true'

s1 =3 
s2 = 3
angle = 90

isPara (s1, s2)
isRec( s1, s2, angle)

CodePudding user response:

as Brian said you need to return values from your functions, you also need to pass isPara to the function isRec.

def isPara(b1, b2):
    '''if base lengths are same, it will return true'''
    return True if b1 == b2 else False

def isRec(angle, isPara):
    '''if isPara is true '''
    return True if isPara is True and angle == 90 else False

s1, s2, angle = 3, 3, 90

isPara = isPara(s1, s2)
print(isRec(angle, isPara))

isPara is provided with 2 variables, neither of which you use inside the function, you changed the variables to b1 and b2. If you pass a variable to a function and want to change it's name in the function use

def isPara(b1, b2):

Also since isRec uses neither s1 nor s2, you do not need to pass those values to the isRec function.

Output from this script:

True

CodePudding user response:

isPara() is a function with 2 arguments so you need to call it accordingly from isRec(). Few updates in your code and it works as expected:

def isPara(s1, s2):
    '''if base lengths are same, it will return true'''
    isPara = False
    if s1 == s2:
        isPara = True
        print ('True')
    else:
        isPara = False
        print ('False')
    return isPara

def isRec(s1, s2, angle):
    '''if isPara is true '''
    if isPara(s1, s2):           <<< Here is the change, call function
        if angle == 90:
            isRec = True
            print ('True')
    else:
        isRec = False
        print ('Not true')

s1 =3 
s2 = 3
angle = 90

isPara (s1, s2)
isRec( s1, s2, angle)

Output:

True
True
True
  • Related