פייתון/פייתון גרסה 3/השוואה בין שלושה ערכים

צרו תכנית אשר תשווה בין ערך הראשי () לבין שלושה ערכים . במידה ויש לפחות שני ערכים מתוך שלושת הערכים שגדולים מהערך הראשי הפונקציה תחזיר אמת.

דרך ארוכה עריכה

def compare_three_objects(main_obj, obj_one, obj_two, obj_three):
    #  two objects are higher than main_obj:
    if ((main_obj < obj_one) and (main_obj < obj_two)) or ((main_obj < obj_one) and (main_obj < obj_three)) :
        return True
    elif (main_obj < obj_two) and (main_obj < obj_three):
        return True
    # all three objects are higher than main_obj:
    elif (main_obj < obj_one) and (main_obj < obj_two) and (main_obj < obj_three):
        return True
    else:
        return False

דרך קצרה עריכה

הדרך הקצרה נעזר ב- :

def compare_three_objects(main_obj, obj_one,obj_two, obj_three):
    counter_temp = 0
    if main_obj < obj_one:
        counter_temp+=1
    if main_obj < obj_two:
        counter_temp += 1
    if main_obj < obj_three:
        counter_temp += 1

    if counter_temp>=2:
        return True
    else:
        return False