Home > Software design >  if statement not returning the right value
if statement not returning the right value

Time:08-20

I have written some code to test whether a bolt should pass, reject, or be sold to amart, which must follow these rules

A bolt must have a tensile strength of at least 400 MPa to pass A bolt that is strong enough must also have a diameter variation of no more than 75 micrometres in order to pass

If a bolt doesn’t pass, but has a diameter variation less than 125 micrometres, and there is an Amart Furniture nearby, then the company will sell it to Amart Furniture instead of discarding it

Any bolt that does not pass and is not sold to Amart Furniture is rejected PASS, REJECT, AMART = 'Pass', 'Reject', 'Sell to Amart'

def q7_assess_bolt(strength, diameter_error, amart_near):
    '''Returns one of three string messages depending on whether a
    bolt is of acceptable quality or not.
    Parameters:
    strength - the bolt's tensile strength in MPa
    diameter_error - the bolt's diameter variation in micrometres
    amart_near - True iff there is an Amart Furniture near the factory
    '''
   if strength >= 400:
    diameter_error <= 75
    return PASS
else:
    if strength < 400:
        diameter_error < 125
        amart_near is True
        return AMART
    else:
        if amart_near is False:
            diameter_error <125
            strength < 400
            return REJECT

whenever i test the values q7_assess_bolt(300, 60, False) the output is 'Sell to Amart' and not Reject like it should be

CodePudding user response:

You are missing the and's in your if condition statements.

Try reformatting your conditions like this:

if strength < 400 and diameter_error < 125 and amart_near is True:
    return AMART

CodePudding user response:

I think " q7_assess_bolt(300, 60, False) the output is 'Sell to Amart' " this doesn't work as you have written " the company will sell it to Amart Furniture if there is an Amart Furniture nearby " but the 3rd argument of the function q7_assess_bolt(), amart_near is set to false, thus it shows no logic & it will show 'reject' messsage. So the 3rd argument should be changed to 'True', inorder to get the result as 'Sell to Amart'

And I have done some changes to your code as per the above question.

def q7_assess_bolt(strength, diameter_error, amart_near):
    passed = False
    if strength >= 400 and diameter_error <= 75:
        passed = True
    if not passed and strength < 400 and diameter_error < 125 and amart_near is True:
        print("Sell to Amart")
    else:
        print("Reject")       

q7_assess_bolt(300, 60, True) # False changed to True
  • Related